MoinMoin Logo
  • Comments
  • Immutable Page
  • Menu
    • Navigation
    • RecentChanges
    • FindPage
    • Local Site Map
    • Help
    • HelpContents
    • HelpOnMoinWikiSyntax
    • Display
    • Attachments
    • Info
    • Raw Text
    • Print View
    • Edit
    • Load
    • Save
  • Login

Navigation

  • Start
  • Sitemap
Revision 47 as of 2020-06-20 12:47:20
  • Java
  • JEE

JEE

Java Platform, Enterprise Edition

JEE version

Application Server

=====================================

JEE5

JBoss5 Glassfish2.1

JEE6

Wildfly7.1 Glassfish3.x

JEE7

y

JEE8

y

JEE 7 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.

  • cd /tmp
  • wget http://download.oracle.com/otn-pub/java/jdk/8u25-b17/jdk-8u25-linux-x64.tar.gz

  • wget http://dlc.sun.com.edgesuite.net/glassfish/4.1/release/glassfish-4.1.zip

  • unzip glassfish-4.1.zip
  • tar xvzf jdk-8u25-linux-x64.tar.gz
  • mv jdk1.8.0_25/ /usr/java
  • mv glassfish4/ /opt/appsrv/
  • cd /usr/java/jdk1.8.0_25
  • chmod 555 * -R
  • /usr/java/jdk1.8.0_25/bin/java -version
  • cd /opt/appsrv/glassfish4/bin
  • JAVA_HOME=/usr/java/jdk1.8.0_25 AS_JAVA=/usr/java/jdk1.8.0_25 asadmin start-domain
  • http://localhost:4848/

  • asadmin change-admin-password
  • asadmin enable-secure-admin
  • asadmin stop-domain
  • asadmin start-domain

Install Glassfish 4.1 on FreeBSD 10.3

Uses MariaDB instead of MySQL.

  • pkg install glassfish-4.1
  • Add glassfish_enable="YES" in /etc/rc.conf.
  • pkg install elinks
  • elinks http://localhost:8080

  • cd /usr/local/glassfish-4.1/bin
  • ./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.
  • ./asadmin enable-secure-admin

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.
  • /usr/local/etc/rc.d/glassfish restart
  • pkg install mariadb101-server-10.1.14 # mariadb
  • vi /etc/rc.conf # mysql_enable="YES"
  • cd /usr/local/etc/rc.d
  • ./mysql-server start
  • mysql_secure_installation # set mysql root pass ...

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
  • cd /usr/local/glassfish-4.1/glassfish/domains/domain1/lib
  • wget http://central.maven.org/maven2/org/mariadb/jdbc/mariadb-java-client/1.5.2/mariadb-java-client-1.5.2.jar

  • jms resources, destination resources
  • JNDI name: jms/Queuex
  • physical destination name: Queuex
  • JMS connection factory
  • type: javax.jms.ConnectionFactory

  • JNDI name: jms/ConnectionFactory
  • JDBC connection pool
  • Pool name: MariaDBPool
  • resource type: javax.sql.DataSource

  • Datasource class name: org.mariadb.jdbc.MariaDbDataSource

  • Connection Validation: Required
  • Validation method meta-data
  • user mysqljdbc
  • userName mysqljdbc
  • databaseName mysqljdbc
  • PortNumber 3306

  • Password 12345678
  • ServerName localhost

  • LoginTimeout 0

  • JDBC resource
  • jndi name: jdbc/mysql
  • connection pool: MariaDBPool

Docker Ubuntu image install

  • docker pull ubuntu
  • docker create --name containerx -p 1022:22 -p 1848:4848 -p 1080:8080 ubuntu
  • docker start containerx
  • docker exec -t -i containerx
  • apt-get update
  • apt-get install tzdata-java openjdk-7-jdk wget unzip
  • java
  • java -version
  • apt-get install
  • cd /tmp
  • wget http://dlc.sun.com.edgesuite.net/glassfish/4.1/release/glassfish-4.1.zip

  • unzip glassfish-4.1.zip
  • mkdir -p /opt/appsrv
  • mv glassfish4/ /opt/appsrv/
  • cd /opt/appsrv/glassfish4/
  • bin/asadmin start-domain
  • bin/asadmin change-admin-password
  • bin/asadmin enable-secure-admin
  • bin/asadmin stop-domain
  • bin/asadmin start-domain
  • 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

<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">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.allowed.bitarus</groupId>
    <artifactId>jmsclient</artifactId>
    <version>0.0.1</version>
    <packaging>jar</packaging> 
    <dependencies>
        <dependency>
            <groupId>org.glassfish.mq</groupId>
            <artifactId>imq</artifactId>
            <version>5.1</version>
        </dependency>   
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>org.allowed.bitarus.JmsClient</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id> 
                        <phase>package</phase> 
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>  
</project>

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

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

Check used tcp ports by OpenMQ

  • nc openmqServer 7676
  • 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:

  • JMSDeliveryMode
  • JMSPriority
  • JMSMessageID
  • JMSTimestamp
  • JMSCorrelationID
  • JMSType

Glassfish 5.0.1

  • MoinMoin Powered
  • Python Powered
  • GPL licensed
  • Valid HTML 4.01