users@glassfish.java.net

Integrating glassfish and jboss messaging using generic ra

From: <glassfish_at_javadesktop.org>
Date: Thu, 09 Jul 2009 00:41:31 PDT

Hello,

Last week i have integrated JBoss Messaging with GlassFish V2 using Generic RA. The only blog that i found is from "Ramesh Parthasarathy" written on 2006. http://weblogs.java.net/blog/rampsarathy/archive/2006/12/configuring_gla.html

Though it was very helpful i found it bit difficult for a newbie to configure. Hope my findings will help you all in setting up JBoss Messaging on GlassFish V2 using Generic RA(Windows/Linux). Please refer ramesh's blog to make yourself aware of the concept.

Install JBoss AS (JBoss Messaging is the default JMS implementation in JBoss AS). Yes its possible to have a standalone JBoss messaging server(No JBoss AS required), but to make things simple i have used the JBoss application server.
(It's always better not to have spaces in your installation path folder names. Comment applicable for glassfish installation too.)

Download JBoss AS 5.0 from here.
http://downloads.sourceforge.net/jboss/jboss-5.0.0.CR2-jdk6.zip?modtime=1221686600&big_mirror=1

You can refer the installation guide for setting up the JDK and the necessary environment variables (JAVA_HOME, JBOSS_HOME, and PATH).
http://www.jboss.org/file-access/default/members/jbossas/freezone/docs/Installation_And_Getting_Started_Guide/5/pdf/Installation_And_Getting_Started_Guide.pdf

Test you AS installation (default configuration)
Incase your GlassFish is using port 8080 then change the JBoss port in server.xml
jbossinstallationdirectory/server/default/deploy/jbossweb.sar/server.xml

<Connector protocol="HTTP/1.1" port="3000" address="${jboss.bind.address}"
               connectionTimeout="20000" redirectPort="8443" />
                           
(Here I have changed the JBoss AS to use port number 3000)
 
Change directory to the jbossinstallationdirectory/bin and execute the run.bat (for Windows) and run.sh (for Linux) script, as appropriate for your operating system.
Now open http://localhost:3000 in your web browser.

If you want to access the server by specifying the IP then execute run.bat -b 0.0.0.0 (for Windows) and run.sh -b 0.0.0.0(for Linux) script.

You should be able to see the server console by opening http://<yourip>:3000 in your web browser.


Change directory to the glassfishinstallationdirectory/bin and execute the following commands.

[b](For Windows)[/b]

(You should update 10.10.10.104 with your IP)
asadmin create-resource-adapter-config --user admin --property SupportsXA=true:ProviderIntegrationMode=jndi:UserName=guest:Password=guest:RMPolicy=OnePerPhysicalConnection:JndiProperties=java.naming.packaging.url.pkgs\=org.jboss.naming\:org.jnp.interfaces,java.naming.factory.initial\=org.jnp.interfaces.NamingContextFactory,java.naming.provider.url\=jnp\:\/\/10.10.10.104\:1099:LogLevel=FINEST genericra

(My glassfish installation is on C:\glassfish-v2ur2)
asadmin deploy --user admin "C:\glassfish-v2ur2\lib\addons\resourceadapters\genericjmsra\genericra.rar"

asadmin create-connector-connection-pool --raname genericra --connectiondefinition javax.jms.QueueConnectionFactory --transactionsupport XATransaction --property ConnectionFactoryJndiName=/XAConnectionFactory inpool

asadmin create-connector-resource --poolname inpool jms/MyQCF

asadmin create-admin-object --raname genericra --restype javax.jms.Queue --property DestinationJndiName=/queue/InQueue jms/InQueue

asadmin create-admin-object --raname genericra --restype javax.jms.Queue --property DestinationJndiName=/queue/OutQueue jms/OutQueue


[b](for Linux)[/b]

(You should update 10.10.10.104 with your IP)
./asadmin create-resource-adapter-config --user admin --property SupportsXA=true:ProviderIntegrationMode=jndi:UserName=guest:Password=guest:RMPolicy=OnePerPhysicalConnection:JndiProperties="java.naming.packaging.url.pkgs\=org.jboss.naming\:org.jnp.interfaces,java.naming.factory.initial\=org.jnp.interfaces.NamingContextFactory,java.naming.provider.url\=jnp\:\/\/10.10.10.104\:1099":LogLevel=FINEST genericra

(My glassfish installation is on /usr/local/glassfish)
./asadmin deploy --user admin /usr/local/glassfish/lib/addons/resourceadapters/genericjmsra/genericra.rar

./asadmin create-connector-connection-pool --raname genericra --connectiondefinition javax.jms.QueueConnectionFactory --transactionsupport XATransaction --property ConnectionFactoryJndiName=/XAConnectionFactory inpool

./asadmin create-connector-resource --poolname inpool jms/MyQCF

./asadmin create-admin-object --raname genericra --restype javax.jms.Queue --property DestinationJndiName=/queue/InQueue jms/InQueue

./asadmin create-admin-object --raname genericra --restype javax.jms.Queue --property DestinationJndiName=/queue/OutQueue jms/OutQueue


Make directory "jbosslib" and copy the following jars into it.

jbossinstallationdirectory/server/default/lib/jboss-messaging.jar
jbossinstallationdirectory/server/default/lib/jnpserver.jar
jbossinstallationdirectory/server/default/lib/jboss-serialization.jar
jbossinstallationdirectory/server/default/lib/log4j.jar
jbossinstallationdirectory/server/default/lib/jboss-remoting.jar
jbossinstallationdirectory/lib/jboss-common-core.jar
jbossinstallationdirectory/lib/jboss-aop-jboss5.jar
jbossinstallationdirectory/lib/javassist.jar
jbossinstallationdirectory/lib/concurrent.jar
jbossinstallationdirectory/lib/trove.jar
jbossinstallationdirectory/lib/jboss-aop-jdk50.jar
jbossinstallationdirectory/lib/jboss-logging-log4j.jar
jbossinstallationdirectory/lib/jboss-logging-spi.jar
jbossinstallationdirectory/lib/jboss-mdr.jar

Create two queues("/queue/InQueue","/queue/OutQueue") using the JBoss AS 5 console (http://<ip>:3000/jmx-console/)

Click on the jboss.messaging on the left panel, select service=ServerPeer.
Scroll down until you see the "deployQueue" operation.

[b]Create the first queue.[/b]
Give some name but the jndiName should be "/queue/InQueue" for this sample project to work.

[b]Create the second queue.[/b]
jndiName should be "/queue/OutQueue" for this sample project to work.

You can access the newly created queue settings from "jboss.messaging.destination" on the console left panel.


That's it! Now you can create your projects for testing these configurations.
Netbeans 6.5 is really a good IDE and its free!

You need to create a sender(Enterprise Application client), requestMDB(EJB Module) and responseMDB(EJB Module).

sender-> Sends the message to the /queue/InQueue(actual JBoss queue) using jms/InQueue(glassfish) resource connector. Note that all queue accessing will be through the glassfish resource connectors.
requestMDB->Listens to InQueue, Gets the message from InQueue, Do something like Web Service Invocation, Send the response to /queue/OutQueue(if required).
responseMDB->Listens to OutQueue, Gets the message from OutQueue, Do something like Database Write Operation.

[b]Create sender[/b](Enterprise Application client) and put this code.


public class Main {

    public static void main(String[] args) {
        try {
            InitialContext ctx = new InitialContext();
            System.out.println("Ctx " + ctx.toString());
            QueueConnectionFactory fac = (QueueConnectionFactory) ctx.lookup("jms/MyQCF");
            System.out.println("Got connection factory jms/MyQCF");
            Queue q = (Queue) ctx.lookup("jms/InQueue");
            System.out.println("Got jms/InQueue");
            QueueConnection con = fac.createQueueConnection("guest", "guest");
            System.out.println("Got Con");
            javax.jms.QueueSession session = con.createQueueSession(true, 0);
            System.out.println("Got Session");
            QueueSender sender = session.createSender(q);
            System.out.println("Got Sender");
            con.start();
            int i = 0;
            for (i = 0; i < 2; i++) {
                Message message = session.createTextMessage("new message: " + i);
                sender.send(message);
                System.out.println("Sent msg to /queue/InQueue: new message" + i);
                session.commit();
            }
            System.out.println("Total message sent to /queue/InQueue: " + i);
            session.close();
            con.close();
            System.exit(1);
        } catch (NamingException ex) {
            ex.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Add the jars(from the jbosslib folder that we have created) to the project.
This sender will send two messages to /queue/InQueue using the jms/InQueue glassfish resource connector.


[b]Create requestMDB[/b](EJB Module project) and put this code into the onMessage function.
Create a package "ejb" and add the class "NewMessageBean"

[b]NewMessageBean.java[/b]

package ejb;

import javax.ejb.MessageDrivenBean;
import javax.ejb.MessageDrivenContext;
import javax.jms.*;

public class NewMessageBean implements MessageDrivenBean,
        MessageListener {

    private transient MessageDrivenContext mdc = null;

    public NewMessageBean() {
        System.out.println("In requestMDB.NewMessageBean()");
    }

    public void setMessageDrivenContext(MessageDrivenContext mdc) {
        System.out.println("In " + "requestMDB.setMessageDrivenContext()");
        this.mdc = mdc;
    }

    public void ejbCreate() {
        System.out.println("In requestMDB.ejbCreate()");
    }

    public void onMessage(Message inMessage) {
                TextMessage msg = (TextMessage) message;
                try {
                        System.out.println("received: " + msg.getText());
                        //Invoke Web Service/Process any business logic/Do Nothing
            InitialContext ctx = new InitialContext();
            System.out.println("Ctx " + ctx.toString());
            QueueConnectionFactory fac = (QueueConnectionFactory) ctx.lookup("jms/MyQCF");
            System.out.println("Got connection factory jms/MyQCF");
            Queue q = (Queue) ctx.lookup("jms/OutQueue");
            System.out.println("Got jms/OutQueue");
            QueueConnection con = fac.createQueueConnection("guest", "guest");
            System.out.println("Got Con");
            javax.jms.QueueSession session = con.createQueueSession(true, 0);
            System.out.println("Got Session");
            QueueSender sender = session.createSender(q);
            System.out.println("Got Sender");
            con.start();
            int i = 0;
            for (i = 0; i < 2; i++) {
                Message message = session.createTextMessage("new response message: " + i);
                sender.send(message);
                System.out.println("Sent msg to /queue/InQueue: new response message" + i);
                session.commit();
            }
            System.out.println("Total message sent to /queue/OutQueue: " + i);
            session.close();
            con.close();
            System.exit(1);
        } catch (NamingException ex) {
            ex.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void ejbRemove() {
        System.out.println("In requestMDB.remove()");
    }
}

Create two files(ejb-jar,sun-ejb-jar) in "\requestMDB\src\conf" folder


[b](ejb-jar.xml)[/b]


<ejb-jar xmlns="http://java.sun.com/xml/ns/j2ee"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
                  http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd"
              version="2.1">

    <display-name>reqQ2MDB</display-name>
    <enterprise-beans>
        <message-driven>
            <ejb-name>requestMDB</ejb-name>
            <ejb-class>ejb.NewMessageBean</ejb-class>
            <transaction-type>Container</transaction-type>
            <message-destination-type>javax.jms.Queue</message-destination-type>
            <message-destination-link>/queue/InQueue</message-destination-link>
            <resource-ref>
                <description>
                    Queue Conn factory where the message will be placed having been consumed.
                </description>
                <res-ref-name>jms/MyQueueConnectionFactory</res-ref-name>
                <res-type>javax.jms.QueueConnectionFactory</res-type>
                <res-auth>Container</res-auth>
                <res-sharing-scope>Shareable</res-sharing-scope>
            </resource-ref>
            <resource-env-ref>
                <description>
The actual queue on the broker that the message is placed onto.
                </description>
                <resource-env-ref-name>jms/OutQueue</resource-env-ref-name>
                <resource-env-ref-type>javax.jms.Queue</resource-env-ref-type>
            </resource-env-ref>
        </message-driven>
    </enterprise-beans>
    <assembly-descriptor>
        <container-transaction>
            <description>This value was set as a default by Sun ONE Studio.</description>
            <method>
                <ejb-name>requestMDB</ejb-name>
                <method-name>*</method-name>
            </method>
            <trans-attribute>Required</trans-attribute>
        </container-transaction>
        <message-destination>
            <display-name>Destination for QueueConsumer</display-name>
            <message-destination-name>/queue/InQueue</message-destination-name>
        </message-destination>
    </assembly-descriptor>
</ejb-jar>


[b](sun-ejb-jar.xml)[/b]

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sun-ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Application Server 8.1 EJB 2.1//EN" "http://www.sun.com/software/appserver/dtds/sun-ejb-jar_2_1-1.dtd">
<sun-ejb-jar>
    <enterprise-beans>
        <ejb>
            <ejb-name>requestMDB</ejb-name>
            <jndi-name>requestMDB</jndi-name>
            <resource-ref>
                <res-ref-name>jms/MyQueueConnectionFactory</res-ref-name>
                <jndi-name>jms/MyQCF</jndi-name>
            </resource-ref>
            <resource-env-ref>
                <resource-env-ref-name>jms/OutQueue</resource-env-ref-name>
                <jndi-name>jms/OutQueue</jndi-name>
            </resource-env-ref>
            <bean-pool>
                <steady-pool-size>10</steady-pool-size>
                <resize-quantity>2</resize-quantity>
                <max-pool-size>30</max-pool-size>
                <pool-idle-timeout-in-seconds>60</pool-idle-timeout-in-seconds>
            </bean-pool>
            <mdb-resource-adapter>
                <resource-adapter-mid>genericra</resource-adapter-mid>
                <activation-config>
                    <activation-config-property>
                        <activation-config-property-name>DestinationType</activation-config-property-name>
                        <activation-config-property-value>javax.jms.Queue</activation-config-property-value>
                    </activation-config-property>
                    <activation-config-property>
                        <activation-config-property-name>MaxPoolSize</activation-config-property-name>
                        <activation-config-property-value>30</activation-config-property-value>
                    </activation-config-property>
                    <activation-config-property>
                        <activation-config-property-name>RedeliveryAttempts</activation-config-property-name>
                        <activation-config-property-value>3</activation-config-property-value>
                    </activation-config-property>
                    <activation-config-property>
                        <activation-config-property-name>RedeliveryInterval</activation-config-property-name>
                        <activation-config-property-value>1</activation-config-property-value>
                    </activation-config-property>
                    <activation-config-property>
                        <activation-config-property-name>ReconnectAttempts</activation-config-property-name>
                        <activation-config-property-value>1000</activation-config-property-value>
                    </activation-config-property>
                    <activation-config-property>
                        <activation-config-property-name>ReconnectInterval</activation-config-property-name>
                        <activation-config-property-value>1</activation-config-property-value>
                    </activation-config-property>
                    <activation-config-property>
                        <activation-config-property-name>DestinationJndiName</activation-config-property-name>
                        <activation-config-property-value>/queue/InQueue</activation-config-property-value>
                    </activation-config-property>
                    <activation-config-property>
                        <activation-config-property-name>ConnectionFactoryJndiName</activation-config-property-name>
                        <activation-config-property-value>java:/XAConnectionFactory</activation-config-property-value>
                    </activation-config-property>
                </activation-config>
            </mdb-resource-adapter>
        </ejb>
    </enterprise-beans>
</sun-ejb-jar>
Add the jars(from the jbosslib folder that we have created) to the project.


You can [b]create the responseMDB[/b] just like the above(Print the message contents in the "onMessage" method. Instead of "InQueue", use "OutQueue" in the xml's).

Now you may run the applications. Use the glassfish admin console to enable/disable the MDB's so that you can track the message flow.

[b]Running the applications.[/b]
1. Deploy all applications to glassfish.
2. Disable both MDB's using glassfish admin console.
3. Make sure that the jboss AS is running (http://<ip>:3000)
4. Run the application client.
5. Check the "MessageCount" for "InQueue" using the jboss AS console. (Should be 1)
6. Enable the requestMDB.
7. Check the "MessageCount" for "InQueue" using the jboss AS console. (Should be 0)
8. Check the "MessageCount" for "OutQueue" using the jboss AS console. (Should be 1)
9. Enable the responseMDB.
10. Check the "MessageCount" for "OutQueue" using the jboss AS console. (Should be 0)
11. Check the glassfish server logs for any errors/results.
12. Done dude. Start experimenting jboss and glassfish. Hell lot of exceptions is waiting for you ;)



[b]Suggestions:[/b]

1. Make use of glassfish server logs for errors/prints.
2. Install "HermesJMS" for tracking the queue messages( http://www.hermesjms.com/confluence/display/HJMS/Home I can help you to configure this, just shoot).
3. Restarting glassfish can solve some issues.(My PL used to say "Restart is the mother of all solutions" :D )
4. Avoid spaces in folder names.
5. Configure/Run it one by one.

6. Instead of adding the jars to each project, you can try adding the jars to the glassfish classpath suffix(Though i have added the jars to my glassfish classpath suffix, the MDB throwed some "javax.resource.ResourceException: Failed to invoke" exception which i was not able to resolve [http://forums.java.net/jive/thread.jspa?threadID=63697]).

Adding the jars to the glassfish classpath suffix

(For Windows)
I have copied the jars to "C:/jbosslib" so that i can specify the following in my glassfish classpath suffix(The jars are separated with ';' ).
C:/jbosslib/jboss-messaging.jar;C:/jbosslib/jnpserver.jar;C:/jbosslib/jboss-common-core.jar;C:/jbosslib/jboss-aop-jboss5.jar;C:/jbosslib/jboss-remoting.jar;C:/jbosslib/javassist.jar;C:/jbosslib/concurrent.jar;C:/jbosslib/trove.jar;C:/jbosslib/jboss-serialization.jar;C:/jbosslib/log4j.jar;C:/jbosslib/jboss-aop-jdk50.jar;C:/jbosslib/jboss-logging-log4j.jar;C:/jbosslib/jboss-logging-spi.jar;C:/jbosslib/jboss-mdr.jar


(For Linux)
I have copied the jars to "/usr/jboss/jboss-5.0.0.CR2/jbosslib" so that i can specify the following in my glassfish classpath suffix(The jars are separated with ':').
/usr/jboss/jboss-5.0.0.CR2/jbosslib/jboss-messaging.jar:/usr/jboss/jboss-5.0.0.CR2/jbosslib/jnpserver.jar:/usr/jboss/jboss-5.0.0.CR2/jbosslib/jboss-common-core.jar:/usr/jboss/jboss-5.0.0.CR2/jbosslib/jboss-aop-jboss5.jar:/usr/jboss/jboss-5.0.0.CR2/jbosslib/jboss-remoting.jar:/usr/jboss/jboss-5.0.0.CR2/jbosslib/javassist.jar:/usr/jboss/jboss-5.0.0.CR2/jbosslib/concurrent.jar:/usr/jboss/jboss-5.0.0.CR2/jbosslib/trove.jar:/usr/jboss/jboss-5.0.0.CR2/jbosslib/jboss-serialization.jar:/usr/jboss/jboss-5.0.0.CR2/jbosslib/log4j.jar:/usr/jboss/jboss-5.0.0.CR2/jbosslib/jboss-aop-jdk50.jar:/usr/jboss/jboss-5.0.0.CR2/jbosslib/jboss-logging-log4j.jar:/usr/jboss/jboss-5.0.0.CR2/jbosslib/jboss-logging-spi.jar:/usr/jboss/jboss-5.0.0.CR2/jbosslib/jboss-mdr.jar


[b]Request:[/b]

If you find anything confusing in this blog, please point it out and we can make this easier for others. Thanks :)
[Message sent by forum member 'rubycube' (rubycube)]

http://forums.java.net/jive/thread.jspa?messageID=354845