JEE

Java Platform, Enterprise Edition

JEE version

Application Server

JEE5

JBoss5 Glassfish2.1

JEE6

JBossAS7 Wildfly7.1 Glassfish3.x JBossEAP6

JEE7

Wildfly8 Glassfish4.x

JEE8

Wildfly14 Glassfish5.x

JEE7 Tutorial

https://docs.oracle.com/javaee/7/tutorial/index.html

Key features: https://blogs.oracle.com/arungupta/entry/java_ee_7_key_features

Reference implementation: https://glassfish.java.net/getstarted.html

Java EE7 APIs: https://docs.oracle.com/javaee/7/tutorial/overview007.htm

Install Glassfish 4.1

https://glassfish.java.net/download.html

http://download.java.net/glassfish/4.1.1/release/glassfish-4.1.1.zip

JDK 8 u20 or above is recommended for GlassFish 4.1.

   1 cd /tmp
   2 wget http://download.oracle.com/otn-pub/java/jdk/8u25-b17/jdk-8u25-linux-x64.tar.gz
   3 wget http://dlc.sun.com.edgesuite.net/glassfish/4.1/release/glassfish-4.1.zip
   4 unzip glassfish-4.1.zip
   5 tar xvzf jdk-8u25-linux-x64.tar.gz
   6 mv jdk1.8.0_25/ /usr/java
   7 mv glassfish4/ /opt/appsrv/
   8 cd /usr/java/jdk1.8.0_25
   9 chmod 555 * -R
  10 /usr/java/jdk1.8.0_25/bin/java -version
  11 cd /opt/appsrv/glassfish4/bin
  12 JAVA_HOME=/usr/java/jdk1.8.0_25 AS_JAVA=/usr/java/jdk1.8.0_25 asadmin start-domain
  13 # http://localhost:4848/
  14 asadmin change-admin-password
  15 asadmin enable-secure-admin
  16 asadmin stop-domain
  17 asadmin start-domain

Install Glassfish 4.1 on FreeBSD 10.3

Uses MariaDB instead of MySQL.

   1 pkg install glassfish-4.1
   2 # Add glassfish_enable="YES" in /etc/rc.conf.
   3 pkg install elinks
   4 elinks http://localhost:8080
   5 cd /usr/local/glassfish-4.1/bin
   6 ./asadmin change-admin-password

Enter admin user name [default: admin]>
Enter the admin password>    #is empty !!!!
Enter the new admin password>
Enter the new admin password again>
Command change-admin-password executed successfully.

Enter admin user name>  admin
Enter admin password for user "admin">
You must restart all running servers for the change in secure admin to take effect.
Command enable-secure-admin executed successfully.

   1 /usr/local/etc/rc.d/glassfish restart
   2 pkg install mariadb101-server-10.1.14 # mariadb
   3 vi /etc/rc.conf # mysql_enable="YES"
   4 cd /usr/local/etc/rc.d
   5 ./mysql-server start
   6 mysql_secure_installation # set mysql root pass ...
   7 

mysql -u root -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 10.1.14-MariaDB FreeBSD Ports

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> create database mysqljdbc;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> create user 'mysqljdbc'@'%' identified by '12345678';
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> GRANT ALL PRIVILEGES ON mysqljdbc.* TO 'mysqljdbc'@'%';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> exit
Bye

   1 cd /usr/local/glassfish-4.1/glassfish/domains/domain1/lib
   2 wget http://central.maven.org/maven2/org/mariadb/jdbc/mariadb-java-client/1.5.2/mariadb-java-client-1.5.2.jar
   3 # jms resources, destination resources 
   4 # JNDI name: jms/Queuex
   5 # physical destination name: Queuex
   6 # JMS connection factory
   7 # type: javax.jms.ConnectionFactory
   8 # JNDI name: jms/ConnectionFactory
   9 # JDBC connection pool 
  10 # Pool name: MariaDBPool        
  11 # resource type: javax.sql.DataSource   
  12 # Datasource class name: org.mariadb.jdbc.MariaDbDataSource
  13 # Connection Validation: Required 
  14 # Validation method meta-data 
  15 # user mysqljdbc
  16 # userName mysqljdbc
  17 # databaseName mysqljdbc
  18 # PortNumber 3306
  19 # Password 12345678
  20 # ServerName localhost 
  21 # LoginTimeout 0
  22 # JDBC resource 
  23 # jndi name: jdbc/mysql                         
  24 # connection pool: MariaDBPool
  25 

Docker Ubuntu image install Glassfish4.1

   1 docker pull ubuntu
   2 docker create --name containerx -p 1022:22 -p 1848:4848 -p 1080:8080 ubuntu 
   3 docker start containerx
   4 docker exec -t -i containerx 
   5 apt-get update
   6 apt-get install tzdata-java openjdk-7-jdk wget unzip
   7 java
   8 java -version
   9 apt-get install 
  10 cd /tmp
  11 wget http://dlc.sun.com.edgesuite.net/glassfish/4.1/release/glassfish-4.1.zip
  12 unzip glassfish-4.1.zip
  13 mkdir -p /opt/appsrv
  14 mv glassfish4/ /opt/appsrv/
  15 cd /opt/appsrv/glassfish4/
  16 bin/asadmin start-domain
  17 bin/asadmin change-admin-password
  18 bin/asadmin enable-secure-admin
  19 bin/asadmin stop-domain
  20 bin/asadmin start-domain
  21 exit

https://github.com/sebsto/docker-glassfish4/blob/master/Dockerfile

JEE6 EJB 3.1 sample for Glassfish 3.1.2.2 and JBoss 7.1.1 (EJB + JPA + Web Service + MDB + Schedule)

Also runs with Glashfish 4.1.

Structure:

.
|-- pom.xml
|-- src
|   `-- main
|       |-- java
|       |   `-- org
|       |       `-- allowed
|       |           `-- bitarus
|       |               |-- MessageEntity.java
|       |               |-- ScheduleRemote.java
|       |               |-- ScheduleWS.java
|       |               |-- Scheduler.java
|       |               `-- TestMDB.java
|       `-- resources
|           `-- META-INF
|               |-- ejb-jar.xml
|               `-- persistence.xml

pom.xml

   1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   2   <modelVersion>4.0.0</modelVersion>
   3   <groupId>org.allowed.bitarus</groupId>
   4   <artifactId>ejbjee6</artifactId>
   5   <version>0.0.1</version>
   6   <packaging>ejb</packaging> 
   7   <dependencies>
   8     <dependency>
   9         <groupId>javax</groupId>
  10         <artifactId>javaee-api</artifactId>
  11         <version>6.0</version>
  12         <scope>provided</scope>
  13     </dependency>    
  14   </dependencies>
  15 </project>
  16 <!--
  17 mkdir -p src/main/java/org/allowed/bitarus
  18 touch src/main/java/org/allowed/bitarus/Scheduler.java
  19 touch src/main/java/org/allowed/bitarus/ScheduleRemote.java
  20 touch src/main/java/org/allowed/bitarus/ScheduleWS.java
  21 touch src/main/java/org/allowed/bitarus/TestMDB.java
  22 touch src/main/java/org/allowed/bitarus/MessageEntity.java
  23 mkdir -p src/main/resources/META-INF
  24 touch src/main/resources/META-INF/ejb-jar.xml
  25 touch src/main/resources/META-INF/persistence.xml
  26 
  27 mvn clean compile package
  28 # Tested in GlassFish Server Open Source Edition 3.1.2.2 (build 5)
  29 -->

./src/main/resources/META-INF/ejb-jar.xml

   1 <ejb-jar>
   2 </ejb-jar>

./src/main/resources/META-INF/persistence.xml

   1 <persistence>
   2     <persistence-unit name="puMysql" transaction-type="JTA">
   3         <jta-data-source>jdbc/mysql</jta-data-source>
   4         <properties>
   5             <!--property name="eclipselink.ddl-generation" value="create-or-extend-tables"/-->    
   6             <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
   7             <property name="eclipselink.ddl-generation.output-mode" value="both"/>
   8             <property name="eclipselink.target-database" value="MySQL"/>
   9         </properties>
  10     </persistence-unit>
  11 </persistence>
  12 <!-- 
  13 Copy mysql-connector-java-5.1.30-bin.jar
  14 cp mysql-connector-java-5.1.30-bin.jar /opt/glassfish-3.1.2.2/glassfish/domains/domain1/lib/
  15 mysql -u root -p
  16 create database mysqljdbc;
  17 create user 'mysqljdbc'@'%' identified by '12345678';
  18 GRANT ALL PRIVILEGES ON mysqljdbc.* TO 'mysqljdbc'@'%';
  19 mysql -u mysqljdbc -p
  20 
  21 JDBC Connection Pool
  22   Name: MySQLPool
  23   Resource type: javax.sql.DataSource
  24   Datasource class name: com.mysql.jdbc.jdbc2.optional.MysqlDataSource
  25   Additional properties
  26   portNumber: 3306
  27   databaseName: mysqljdbc
  28   user: mysqljdbc
  29   password: 12345678
  30   serverName: 127.0.0.1
  31 
  32 JDBC Resource:
  33   New, 
  34   Pool Name: MySQLPool
  35   JNDI name: jdbc/mysql
  36 -->

./src/main/java/org/allowed/bitarus/Scheduler.java

   1 package org.allowed.bitarus;
   2 import javax.ejb.Singleton;
   3 import javax.ejb.Schedule;
   4 import javax.ejb.ScheduleExpression;
   5 import javax.annotation.Resource;
   6 import javax.ejb.Timeout;
   7 import javax.ejb.TimerConfig;
   8 import javax.ejb.TimerService;
   9 import javax.ejb.Timer;
  10 import javax.annotation.PostConstruct;
  11 import javax.ejb.Startup;
  12 import javax.jms.ConnectionFactory;
  13 import javax.jms.Queue;
  14 import javax.jms.Connection;
  15 import javax.jms.Session;
  16 import javax.jms.MessageProducer;
  17 import javax.jms.TextMessage;
  18 @Singleton
  19 @Startup
  20 public class Scheduler {
  21     @Resource(mappedName="jms/ConnectionFactory")
  22     private ConnectionFactory connectionFactory;
  23     @Resource(mappedName="jms/Queuex")
  24     private Queue queue;    
  25     
  26     @Resource
  27     private TimerService timerService;
  28 
  29     @PostConstruct
  30     private void postConstruct() {    
  31         TimerConfig config = new TimerConfig("OneMinutePassed", false);
  32         ScheduleExpression se = new ScheduleExpression();
  33         se.minute("*");
  34         se.hour("*");
  35         timerService.createCalendarTimer(se, config);
  36     }
  37 
  38     public String addSchedule(String second,String minute,String hour,String info){
  39         TimerConfig config = new TimerConfig(info, false);
  40         ScheduleExpression se = new ScheduleExpression();
  41         se.second(second);
  42         se.minute(minute);
  43         se.hour(hour);
  44         timerService.createCalendarTimer(se, config);        
  45         return "Added " + info;
  46     }    
  47     
  48     @Schedule(minute="*/5", hour="*")
  49     private void eachFiveMinutes(){
  50         System.out.println("Called each five minute");
  51     }    
  52 
  53     @Timeout
  54     public void timeout(Timer timer) {
  55         if ("OneMinutePassed".equals(timer.getInfo())) {    
  56             System.out.println("OneMinutePassed !!!");
  57             /*for(Timer t : timerService.getTimers() ){
  58                 System.out.println(">>> "+ t.getInfo() );
  59             }*/
  60         }else{
  61             System.out.println("!!! "+ timer.getInfo() );
  62             // send jms message
  63             try{
  64                 Connection connection = connectionFactory.createConnection();
  65                 Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
  66                 MessageProducer mp = session.createProducer(queue);
  67                 TextMessage message = session.createTextMessage();
  68                 message.setText( timer.getInfo().toString() );
  69                 mp.send(message);
  70                 mp.close();
  71                 session.close();
  72                 connection.close();                
  73             }catch(Exception ex){ ex.printStackTrace(); }
  74         }        
  75     }    
  76 }

./src/main/java/org/allowed/bitarus/ScheduleRemote.java

   1 package org.allowed.bitarus;
   2 import javax.ejb.Remote;
   3 
   4 @Remote
   5 public interface ScheduleRemote{
   6     String addSchedule(String second,String minute,String hour,String info);
   7 }

./src/main/java/org/allowed/bitarus/ScheduleWS.java

   1 package org.allowed.bitarus;
   2 import javax.ejb.Stateless;
   3 import javax.jws.WebMethod;
   4 import javax.jws.WebService;
   5 import javax.ejb.ScheduleExpression;
   6 import javax.annotation.Resource;
   7 import javax.ejb.TimerService;
   8 import javax.ejb.TimerConfig;
   9 import javax.ejb.EJB;
  10 @Stateless
  11 @WebService
  12 public class ScheduleWS implements ScheduleRemote{
  13     @Resource
  14     private TimerService timerService;    
  15     @EJB
  16     Scheduler sc;
  17     
  18     @WebMethod
  19     public String addSchedule(String second,String minute,String hour,String info){
  20         return sc.addSchedule(second, minute, hour, info);
  21     }
  22 }

./src/main/java/org/allowed/bitarus/TestMDB.java

   1 package org.allowed.bitarus;
   2 
   3 import javax.ejb.MessageDriven;
   4 import javax.jms.Message;
   5 import javax.jms.TextMessage;
   6 import javax.persistence.PersistenceContext;
   7 import javax.persistence.EntityManager;
   8 @MessageDriven(mappedName="jms/Queuex")
   9 public class TestMDB{
  10     @PersistenceContext(unitName="puMysql")
  11     EntityManager em;
  12     
  13     public void onMessage(Message inMessage) {
  14         try{
  15             String text = ( (TextMessage) inMessage).getText() ;
  16             System.out.println( text );     
  17             MessageEntity me = new MessageEntity();
  18             me.setMessage( text  );
  19             me.setCreationDate( new java.util.Date() );
  20             em.persist(me);
  21         }catch(Exception ex){
  22             ex.printStackTrace();
  23         }
  24     }
  25 }

./src/main/java/org/allowed/bitarus/MessageEntity.java

   1 package org.allowed.bitarus;
   2 
   3 import java.util.Date;
   4 import javax.persistence.Id;
   5 import javax.persistence.Entity;
   6 import javax.persistence.Column;
   7 import javax.persistence.GeneratedValue;
   8 import javax.persistence.GenerationType;
   9 import javax.persistence.TemporalType;
  10 import javax.persistence.Temporal;
  11 @Entity
  12 public class MessageEntity{
  13     @Id
  14     @GeneratedValue(strategy = GenerationType.IDENTITY) 
  15     // @GeneratedValue(strategy = GenerationType.AUTO) 
  16     // @OneToMany 
  17     // @NamedQueries( { @NamedQuery(name="MessageEntity.byId",query="Select me From MessageEntity e where e.id = :id;") , @NamedQuery()  } )
  18     // JPQL Select me From MessageEntity e where e.id = :id;
  19     // Query q = em.createQuery("Select me From MessageEntity e where e.id = :id;");
  20     // Query q = em.createNamedQuery("MessageEntity.byId");
  21     // q.setParameter("id",12);
  22     // List<MessageEntity> res = q.getResultList ();
  23     private long id;
  24     @Column
  25     @Temporal(TemporalType.TIMESTAMP) 
  26     private Date creationDate;
  27     @Column
  28     private String message;
  29     
  30     public long getId(){return this.id;}
  31     public void setId(long id){ this.id=id;}
  32 
  33     public Date getCreationDate(){return this.creationDate;}
  34     public void setCreationDate(Date creationDate){ this.creationDate=creationDate;}
  35     
  36     public String getMessage(){return this.message;}
  37     public void setMessage(String message){ this.message=message;}        
  38 }

Glassfish OpenMQ standalone client

Structure

.
|-- pom.xml
|-- src
|   `-- main
|       `-- java
|           `-- org
|               `-- allowed
|                   `-- bitarus
|                       `-- JmsClient.java

pom.xml

   1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   2     <modelVersion>4.0.0</modelVersion>
   3     <groupId>org.allowed.bitarus</groupId>
   4     <artifactId>jmsclient</artifactId>
   5     <version>0.0.1</version>
   6     <packaging>jar</packaging> 
   7     <dependencies>
   8         <dependency>
   9             <groupId>org.glassfish.mq</groupId>
  10             <artifactId>imq</artifactId>
  11             <version>5.1</version>
  12         </dependency>   
  13     </dependencies>
  14     <build>
  15         <plugins>
  16             <plugin>
  17                 <artifactId>maven-assembly-plugin</artifactId>
  18                 <version>2.4</version>
  19                 <configuration>
  20                     <descriptorRefs>
  21                         <descriptorRef>jar-with-dependencies</descriptorRef>
  22                     </descriptorRefs>
  23                     <archive>
  24                         <manifest>
  25                             <mainClass>org.allowed.bitarus.JmsClient</mainClass>
  26                         </manifest>
  27                     </archive>
  28                 </configuration>
  29                 <executions>
  30                     <execution>
  31                         <id>make-assembly</id> 
  32                         <phase>package</phase> 
  33                         <goals>
  34                             <goal>single</goal>
  35                         </goals>
  36                     </execution>
  37                 </executions>
  38             </plugin>
  39         </plugins>
  40     </build>  
  41 </project>

src/main/java/org/allowed/bitarus/JmsClient.java

   1 package org.allowed.bitarus;
   2 import com.sun.messaging.ConnectionConfiguration;
   3 import com.sun.messaging.ConnectionFactory;
   4 import com.sun.messaging.Queue;
   5 import javax.jms.Connection;
   6 import javax.jms.JMSException;
   7 import javax.jms.Message;
   8 import javax.jms.MessageProducer;
   9 import javax.jms.Session;
  10 
  11 public class JmsClient {
  12     
  13     public static void main( String[] args ) 
  14     {
  15         try{
  16             ConnectionFactory connFactory = new ConnectionFactory();
  17             connFactory.setProperty(ConnectionConfiguration.imqAddressList, "127.0.0.1:7676");
  18             Queue q = new Queue("Queuex");      
  19             Connection connection = connFactory.createConnection(); 
  20             System.out.println("After create connection");
  21             Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 
  22             MessageProducer producer = session.createProducer(q); 
  23             Message message = session.createTextMessage("Bitarus test message");
  24             producer.send(message);
  25             producer.close();
  26             session.close();
  27             connection.close();
  28             System.out.println("After send message");
  29         }catch(Exception ex){
  30             ex.printStackTrace();             
  31         }
  32         
  33         System.out.println("End");
  34     }
  35 }

Check OpenMQ MessageQueue state

   1 cd /opt/glassfish-3.1.2.2/mq/bin
   2 imqcmd list dst  # input the admin user and pass
   3 

Check used tcp ports by OpenMQ

   1 nc openmqServer 7676
   2 telnet openmqServer 7676

Should show in text the available ports, with 7676 on the list and other dynamic port with the text jms tcp normal next to it. That dynamic port should also be added in NAT port forwarding if required together withport 7676.

JMS message selector

JMS Message Selectors

Filter queue messages based on a JMS property value.

Session createConsumer

Creates a MessageConsumer for the specified destination, using a message selector.

   1 // on the producer
   2 myMessage.setStringProperty("PropertyX", "teste");
   3 
   4 // on the consumer to receive messages with the property PropertyX with value teste
   5 sessionx.createConsumer(queuex,"PropertyX='teste'");

Message selector in a MessageDrivenBean

   1 @MessageDriven(mappedName="jms/Queuex", activationConfig =  {
   2   @ActivationConfigProperty(propertyName = "messageSelector",propertyValue = "PropertyX='teste'")
   3 })

From Message

A JMS message selector allows a client to specify, by header field references and property references, the messages it is interested in. Only messages whose header and property values match the selector are delivered.

A message selector matches a message if the selector evaluates to true when the message's header field values and property values are substituted for their corresponding identifiers in the selector.

Message header field references are restricted to JMSDeliveryMode, JMSPriority, JMSMessageID, JMSTimestamp, JMSCorrelationID, and JMSType. 

The following message selector selects messages with a message type of car and color of blue and weight greater than 2500 pounds:
"JMSType = 'car' AND color = 'blue' AND weight > 2500"

JMS header fields:

Glassfish 5.0.1

Java/JEE (last edited 2023-05-29 09:01:58 by 127)