users@glassfish.java.net

Re: Integrating glassfish and jboss messaging using generic ra

From: <glassfish_at_javadesktop.org>
Date: Thu, 09 Jul 2009 02:44:19 PDT

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