Tuesday, April 7, 2015

Message Driven Bean in WebSphere 8.0

This is a quick blog post on how to create and deploy a Message Driven Bean on WebSphere 8.0 using Maven 3. Code for this can be found at https://github.com/jamie3/websphere-mdb.

Project structure


I created the following project structure using maven

+ websphere-mdb
    + websphere-mdb-ear
    + websphere-mdb-ejb
    + websphere-mdb-jar

Top Level POM

websphere-mdb is the top level project which contains the follow modules:
  • websphere-mdb-ear - EAR project
  • websphere-mdb-ejb - EJB project
  • websphere-mdb-jar - 3rd party source code which the EJB uses


I set the compiler level to 1.6 since we are deploying on WAS 8.0.


<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>com.github.jamie3</groupId>
 <artifactId>websphere-mdb</artifactId>
 <version>1.0.0</version>
 <packaging>pom</packaging>
 
 <modules>
  <module>websphere-mdb-jar</module>
  <module>websphere-mdb-ejb</module>
  <module>websphere-mdb-ear</module>
 </modules>
  
 <build>
  <plugins>
   <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
    <configuration>
     <source>1.6</source>
     <target>1.6</target>
    </configuration>
   </plugin>
  </plugins>
 </build> 
  
</project>


EJB Module

The EJB Module uses the maven-ejb-plugin and also has a a dependency on jms and my jar file. I also used the JBOSS repository to load the jms dependency.

The ejb pom can be found at https://github.com/jamie3/websphere-mdb/blob/master/websphere-mdb-ejb/pom.xml.

In the META-INF folder of the EJB I added the ibm-ejb-jar-bnd.xml file which is loaded by IBM WebSphere Application Server during deployment of the EAR.


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

 <artifactId>websphere-mdb-ejb</artifactId>
 <packaging>ejb</packaging>

 <parent>
  <groupId>com.github.jamie3</groupId>
  <artifactId>websphere-mdb</artifactId>
  <version>1.0.0</version>
 </parent>

 <dependencies>
  <dependency>
   <groupId>com.github.jamie3</groupId>
   <artifactId>websphere-mdb-jar</artifactId>
   <version>1.0.0</version>
  </dependency>
  <dependency>
   <groupId>javax.ejb</groupId>
   <artifactId>ejb-api</artifactId>
   <version>3.0</version>
   <scope>provided</scope>
  </dependency>
  <dependency>
   <groupId>javax.jms</groupId>
   <artifactId>jms</artifactId>
   <version>1.1</version>
   <scope>provided</scope>
  </dependency>
 </dependencies>

 <build>
  <plugins>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-ejb-plugin</artifactId>
    <version>2.5</version>
    <configuration>
     <ejbVersion>3.1</ejbVersion>
     <generateClient>false</generateClient>
     <archive>
      <manifest>
       <addClasspath>false</addClasspath>
      </manifest>
     </archive>
    </configuration>
   </plugin>
  </plugins>
 </build>


 <repositories>
  <repository>
   <id>repository.jboss.org-public</id>
   <name>JBoss.org Maven repository</name>
   <url>https://repository.jboss.org/nexus/content/groups/public</url>
  </repository>
 </repositories>

</project>


Create Message Driven Bean

In the EJB module I added a class as my Message Driven Bean.


package ejb;

import jar.Echo;

import javax.annotation.PostConstruct;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.TextMessage;

/**
 * Session Bean implementation class MySessionBean
 */
@MessageDriven(activationConfig = { @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue") }, mappedName = "jms/SomeQueue")
public class MyMDB implements javax.jms.MessageListener {

 Echo echo;
 
    /**
     * Default constructor. 
     */
    public MyMDB() {
        // TODO Auto-generated constructor stub
     
    }

    @PostConstruct
    public void init() {
     System.out.println("initializing ejb");
     echo = new Echo();
    }

 @Override
 public void onMessage(Message message) {
  // TODO Auto-generated method stub
  TextMessage text = (TextMessage)message;
     try {
      String msg = text.getText();
   echo.echo("Message from JMS: " + text.getText());
  } catch (JMSException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
}


JAR Module

The JAR Module contains a simple class which is used by my EJB.


package jar;

public class Echo {

 public void echo(String text) {
  System.out.println(text);
 }
}

The pom for the jar module can be found here https://github.com/jamie3/websphere-mdb/blob/master/websphere-mdb-jar/pom.xml.

EAR Module


Finally the EAR module uses the maven-ear-plugin to add the JAR and EJB as EAR modules. All third party dependencies are packed into the lib folder.



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

 <parent>
  <groupId>com.github.jamie3</groupId>
  <version>1.0.0</version>
  <artifactId>websphere-mdb</artifactId>
 </parent>
 
 <artifactId>websphere-mdb-ear</artifactId>
 <packaging>ear</packaging>

 <dependencies>
  <dependency>
   <groupId>com.github.jamie3</groupId>
   <artifactId>websphere-mdb-ejb</artifactId>
   <version>1.0.0</version>
   <type>ejb</type>
  </dependency>
 </dependencies>

 <build>
  <plugins>
   <plugin>
    <artifactId>maven-ear-plugin</artifactId>
    <version>2.10</version>
    <configuration>
     <version>6</version>
     <defaultLibBundleDir>lib</defaultLibBundleDir>
     <modules>
      <ejbModule>
       <groupId>com.github.jamie3</groupId>
       <artifactId>websphere-mdb-ejb</artifactId>
      </ejbModule>
      <jarModule>
       <groupId>com.github.jamie3</groupId>
       <artifactId>websphere-mdb-jar</artifactId>
      </jarModule>
     </modules>
    </configuration>
   </plugin>
  </plugins>
 </build>
 
 <repositories>
  <repository>
   <id>repository.jboss.org-public</id>
   <name>JBoss.org Maven repository</name>
   <url>https://repository.jboss.org/nexus/content/groups/public</url>
  </repository>
 </repositories>
</project>




No comments: