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

Upload page content

You can upload content for the page named below. If you change the page name, you can also upload content for another page. If the page name is empty, we derive the page name from the file name.

File to load page content from
Page name
Comment

  • Java
  • JEE

Contents

  1. JEE
    1. JEE7 Tutorial
    2. Docker Ubuntu image install Glassfish4.1
    3. JEE6 EJB 3.1 sample for Glassfish 3.1.2.2 and JBoss 7.1.1 (EJB + JPA + Web Service + MDB + Schedule)
    4. Glassfish OpenMQ standalone client
    5. JMS message selector
    6. Glassfish 5.1.0 - Jakarta EE 8 Specification
    7. Glassfish 6 - Jakarta EE 9 specification

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

JEE9

Wildfly25 Glassfish6.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.
  • ./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.

   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:

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

Glassfish 5.1.0 - Jakarta EE 8 Specification

  • https://glassfish.org/docs/5.1.0/quick-start-guide/toc.html

  • https://www.eclipse.org/downloads/download.php?file=/glassfish/glassfish-5.1.0.zip

  • https://adoptium.net/pt-BR/download?link=https%3A%2F%2Fgithub.com%2Fadoptium%2Ftemurin8-binaries%2Freleases%2Fdownload%2Fjdk8u472-b08%2FOpenJDK8U-jdk_x64_linux_hotspot_8u472b08.tar.gz&vendor=Adoptium

   1 # GlassFish 5.1 requires Oracle JDK 8 Update 191.
   2 cd ~/Downloads
   3 tar tvzf OpenJDK8U-jdk_x64_linux_hotspot_8u472b08.tar.gz
   4 unzip -t glassfish-5.1.0.zip
   5 mv OpenJDK8U-jdk_x64_linux_hotspot_8u472b08.tar.gz ~/
   6 mv  glassfish-5.1.0.zip ~/
   7 cd ~/
   8 tar xvzf OpenJDK8U-jdk_x64_linux_hotspot_8u472b08.tar.gz
   9 unzip glassfish-5.1.0.zip
  10 cd ~/glassfish5/bin
  11 
  12 export JAVA_HOME=/home/vitor/jdk8u472-b08 
  13 export AS_JAVA=/home/vitor/jdk8u472-b08
  14 ./asadmin change-admin-password
  15 #Enter admin user name [default: admin]>
  16 #Enter the admin password> <empty pass>
  17 #Enter the new admin password> 
  18 #Enter the new admin password again> 
  19 #Command change-admin-password executed successfully.
  20 
  21 ./asadmin start-domain
  22 # http://localhost:4848/
  23 # http://localhost:4848/common/index.jsf
  24 
  25 ./asadmin stop-domain
  26 ./asadmin start-domain
  27 #Waiting for domain1 to start ...
  28 #Successfully started the domain : domain1
  29 #domain  Location: /home/vitor/glassfish5/glassfish/domains/domain1
  30 #Log File: /home/vitor/glassfish5/glassfish/domains/domain1/logs/server.log
  31 #Admin Port: 4848
  32 #Command start-domain executed successfully.
  33 # Error Could not initialize class sun.security.ssl.SSLEngineImpl in log
  34 # Remove grizzly-npn-bootstrap.jar 
  35 kill $(ps uax | grep glass | head -1 | awk '//{print $2}')
  36 cd ~/glassfish5
  37 rm ./glassfish/modules/endorsed/grizzly-npn-bootstrap.jar
  38 cd bin/
  39 ./asadmin start-domain

As issues with OpenJDK

Glassfish 6 - Jakarta EE 9 specification

Eclipse GlassFish 6.2.5 requires JDK 11 or newer.

  • https://adoptium.net

  • https://adoptium.net/temurin/releases?version=11&os=any&arch=any

   1 cd ~/Downloads
   2 mv glassfish-6.2.5.zip ~/
   3 mv OpenJDK11U-jdk_x64_linux_hotspot_11.0.29_7.tar.gz ~/
   4 
   5 cd ~
   6 unzip glassfish-6.2.5.zip 
   7 tar tvzf OpenJDK11U-jdk_x64_linux_hotspot_11.0.29_7.tar.gz 
   8 tar xvzf OpenJDK11U-jdk_x64_linux_hotspot_11.0.29_7.tar.gz 
   9 
  10 export JAVA_HOME=/home/vitor/jdk-11.0.29+7 
  11 export AS_JAVA=/home/vitor/jdk-11.0.29+7
  12 
  13 cd ~/glassfish6/bin
  14 ./asadmin  change-admin-password
  15 #Enter admin user name [default: admin]>
  16 #Enter the admin password>  <empty pass>
  17 #Enter the new admin password> 
  18 #Enter the new admin password again> 
  19 #Command change-admin-password executed successfully.
  20 
  21 ./asadmin start-domain
  22 # http://localhost:4848/
  23 
  24 ./asadmin enable-secure-admin 
  25 #Enter admin user name>  admin
  26 #Enter admin password for user "admin"> 
  27 #You must restart all running servers for the change in secure admin to take effect.
  28 #Command enable-secure-admin executed successfully.
  29 ./asadmin stop-domain
  30 #Waiting for the domain to stop .
  31 #Command stop-domain executed successfully.
  32 ./asadmin start-domain
  33 #Waiting for domain1 to start ...
  34 #Successfully started the domain : domain1
  35 #domain  Location: /home/vitor/glassfish6/glassfish/domains/domain1
  36 #Log File: /home/vitor/glassfish6/glassfish/domains/domain1/logs/server.log
  37 #Admin Port: 4848
  38 #Command start-domain executed successfully.
  39 # https://localhost:4848/common/index.jsf
  40 podman run --rm -d --name mariadb -p 3306:3306 --env MARIADB_ROOT_PASSWORD=12345678 mariadb:latest
  41 podman exec -it mariadb  bash
  42 mariadb -h localhost -u root -p
  43 # create database mysqljdbc;
  44 # create user 'mysqljdbc'@'%' identified by '12345678';
  45 # GRANT ALL PRIVILEGES ON mysqljdbc.* TO 'mysqljdbc'@'%';
  46 # exit
  47 # exit
  48 # JDBC driver https://repo1.maven.org/maven2/org/mariadb/jdbc/mariadb-java-client/3.5.6/mariadb-java-client-3.5.6.jar
  49 
  50 mkdir -p src/main/java/org/bitarus/
  51 mkdir -p src/main/resources/META-INF
  52 touch pom.xml
  53 touch src/main/java/org/bitarus/Scheduler.java
  54 touch src/main/java/org/bitarus/ScheduleRemote.java
  55 touch src/main/java/org/bitarus/ScheduleWS.java
  56 touch src/main/java/org/bitarus/TestMDB.java
  57 touch src/main/java/org/bitarus/MessageEntity.java
  58 touch src/main/resources/META-INF/ejb-jar.xml
  59 touch src/main/resources/META-INF/persistence.xml

JDBC connection pool and resource

   1 # JDBC connection pool, new 
   2 #   Pool name: MariaDBPool        
   3 #   resource type: javax.sql.DataSource   
   4 # Next
   5 #   Datasource class name: org.mariadb.jdbc.MariaDbDataSource
   6 # Advanced tab, 
   7 #  Connection Validation: Required 
   8 #  Validation method: meta-data 
   9 # Additional properties
  10 #   user mysqljdbc
  11 #   userName mysqljdbc
  12 #   databaseName mysqljdbc
  13 #   PortNumber 3306
  14 #   Password 12345678
  15 #   ServerName localhost 
  16 #   LoginTimeout 0
  17 #   Url jdbc:mariadb://localhost:3306/mysqljdbc
  18 #   useSSL false
  19 # JDBC resource tab
  20 #   jndi name: jdbc/mysql                         
  21 #   connection pool: MariaDBPool
  22 
  23 cp ~/Downloads/mariadb-java-client-3.5.6.jar ~/glassfish6/glassfish/domains/domain1/lib/
  24 cp ~/Downloads/mariadb-java-client-3.5.6.jar ~/glassfish6/glassfish/lib/
  25 ./asadmin stop-domain
  26 ./asadmin start-domain

JMS queue

   1 # Resources
   2 #   jms resources, 
   3 #      destination resources 
   4 #       JNDI name: jms/Queuex
   5 #       physical destination name: Queuex
   6 #       resource type: jakarta.jms.Queue
   7 #       status: true
   8 #   connection factories
   9 #     JMS connection factory
  10 #       Resource Type: jakarta.jms.ConnectionFactory
  11 #       JNDI name: jms/ConnectionFactory
  12 #       status: true
  13 

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.bitarus</groupId>
   4   <artifactId>ejbjee9</artifactId>
   5   <version>0.0.1</version>
   6   <packaging>ejb</packaging> 
   7   <dependencies>
   8     <dependency>
   9       <groupId>jakarta.platform</groupId>
  10       <artifactId>jakarta.jakartaee-api</artifactId>
  11       <version>9.0.0</version>
  12       <scope>provided</scope>
  13     </dependency>
  14   </dependencies>
  15 </project>

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>

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

   1 package org.bitarus;
   2 
   3 import java.util.Date;
   4 import jakarta.persistence.Id;
   5 import jakarta.persistence.Entity;
   6 import jakarta.persistence.Column;
   7 import jakarta.persistence.GeneratedValue;
   8 import jakarta.persistence.GenerationType;
   9 import jakarta.persistence.TemporalType;
  10 import jakarta.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 }

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

   1 package org.bitarus;
   2 import jakarta.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/bitarus/Scheduler.java

   1 package org.bitarus;
   2 import jakarta.ejb.Singleton;
   3 import jakarta.ejb.Schedule;
   4 import jakarta.ejb.ScheduleExpression;
   5 import jakarta.annotation.Resource;
   6 import jakarta.ejb.Timeout;
   7 import jakarta.ejb.TimerConfig;
   8 import jakarta.ejb.TimerService;
   9 import jakarta.ejb.Timer;
  10 import jakarta.annotation.PostConstruct;
  11 import jakarta.ejb.Startup;
  12 import jakarta.jms.ConnectionFactory;
  13 import jakarta.jms.Queue;
  14 import jakarta.jms.Connection;
  15 import jakarta.jms.Session;
  16 import jakarta.jms.MessageProducer;
  17 import jakarta.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/bitarus/ScheduleWS.java

   1 package org.bitarus;
   2 import jakarta.ejb.Stateless;
   3 import jakarta.jws.WebMethod;
   4 import jakarta.jws.WebService;
   5 import jakarta.ejb.ScheduleExpression;
   6 import jakarta.annotation.Resource;
   7 import jakarta.ejb.TimerService;
   8 import jakarta.ejb.TimerConfig;
   9 import jakarta.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     /* 
  19     http://localhost:8080/ScheduleWSService/ScheduleWS?WSDL
  20     http://localhost:8080/ScheduleWSService/ScheduleWS?Tester
  21     addSchedule("0", "*/2" , "0" , "infox1" ) 
  22     */
  23     @WebMethod
  24     public String addSchedule(String second,String minute,String hour,String info){
  25         return sc.addSchedule(second, minute, hour, info);
  26     }
  27 }

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

   1 package org.bitarus;
   2 
   3 import jakarta.ejb.MessageDriven;
   4 import jakarta.jms.Message;
   5 import jakarta.jms.TextMessage;
   6 import jakarta.persistence.PersistenceContext;
   7 import jakarta.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 }
  • MoinMoin Powered
  • Python Powered
  • GPL licensed
  • Valid HTML 4.01