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 17 as of 2015-02-16 22:43:58
  • Java
  • JEE

JEE

Java Platform, Enterprise Edition

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

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

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)

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 mkdir -p src/main/resources/META-INF
  22 touch src/main/resources/META-INF/ejb-jar.xml
  23 mvn clean compile package
  24 # Tested in GlassFish Server Open Source Edition 3.1.2.2 (build 5)
  25 -->

ejb-jar.xml

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

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

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 }

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 }

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 }

TestMDB.java

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

MessageEntity.java

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

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
  • MoinMoin Powered
  • Python Powered
  • GPL licensed
  • Valid HTML 4.01