Shoshin - Steve's blog

Using API to send/receive messages from a client to JavaMQ

jeudi juin 26, 2008

Download first the project zipped.

Set the project classpath as such (as mentionned in the README.txt located at \FAST_JMS_ToolBox\src):



  • S:\\netbeans\\java2\\ant\\lib\\ant-apache-log4j.jar

  • S:\\netbeans\\java2\\ant\\lib\\ant.jar

  • S:\\appserver\\lib\\javaee.jar

  • S:\\appserver\\lib\\appserver-rt.jar

  • S:\\appserver\\lib\\aappserv-ext.jar

  • S:\\appserver\\lib\\appserv-admin.jar

  • S:\\appserver\\lib\\appserv-deployment-client.jar

  • S:\\appserver\\imq\\lib\\fscontext.jar

  • S:\\appserver\\imq\\lib\\imq.jar

  • S:\\appserver\\imq\\lib\\jms.jar

  • imqjmsra.jar from S:\appserver\imq\lib\imqjmsra.rar

  • S:\appserver\domains\domain1\jbi\components\sun-jms-binding\install_root\lib\com.stc.jmsjca.raunifiedjms.jar

  • C:\\ApacheGroup\\apache-tomcat-5.5.20\\webapps\\axis\\WEB-INF\\lib\\log4j-1.2.8.jar

  • C:\\ApacheGroup\\apache-tomcat-5.5.20\\webapps\\axis\\WEB-INF\\lib\\commons-logging-1.0.4.jar



In case you need to talk to JavaMQ from your client, here are some
examples showing the interfaces to use as well as the parameters to
set, let's start for instance with   com.sun.fast.toolbox.jms.Sender.java :


The main method will check the parameters you defined , especially :



  • the host name

  • the message payload or the file name if you want to send the file content as the payload

  • the port number

  • the queue name

  • the client ID

  • user name & password if necessary



 The main then invokes the sendMessage() method which creates the connection plumbing as shown in the here under snippet :

qcf = new com.sun.messaging.QueueConnectionFactory();
// set hostName, port
((com.sun.messaging.QueueConnectionFactory)qcf).setProperty(com.sun.messaging.ConnectionConfiguration.imqBrokerHostName, getHostName());
((com.sun.messaging.QueueConnectionFactory)qcf).setProperty(com.sun.messaging.ConnectionConfiguration.imqBrokerHostPort, String.valueOf(getPort()));

((com.sun.messaging.QueueConnectionFactory)qcf).setProperty(com.sun.messaging.ConnectionConfiguration.imqConfiguredClientID, getClientId());

if ( !getUserName().equalsIgnoreCase( "" )) {
   // authentication required
   if (getPassword().equalsIgnoreCase( "" ))
     throw new Exception ("Password is NOT set ! " );
   else queueConnection = qcf.createQueueConnection(getUserName(), getPassword());
}
else {
  queueConnection = qcf.createQueueConnection();
}

queueConnection.start();          
queueSession = queueConnection.createQueueSession(true, Session.AUTO_ACKNOWLEDGE);
queue = queueSession.createQueue(getQueueName());
producer = queueSession.createSender(queue);


TextMessage txtMsg = queueSession.createTextMessage();
txtMsg.setText(getPayload());
producer.send(txtMsg);


Next example , look at com.sun.fast.toolbox.jms.asadmin.create-jms-resource.bat to create a Queue Connection Factory :
S:\appserver\bin\asadmin.bat create-jms-resource --restype javax.jms.QueueConnectionFactory jms/qConnectionFactory


Check it with :
S:\appserver\bin\asadmin.bat list-connector-resources
S:\appserver\bin\asadmin.bat asadmin list-jms-resources


You will also see the qConnectionFactory appear in the admin console and in the JNDI Browser :






Looking at com.sun.fast.toolbox.jms.ra.Sender.java , I added 3 fields:


    private int orbPort = 3100;
    private String JNDI_CONNECTION_FACTORY_NAME = "qConnectionFactory" ;
    private String JNDI_JMS_SUB_CONTEXT = "jms" ;


In the getInitialContext() method you notice the properties to add in the hashtable :
            ht.put(Context.INITIAL_CONTEXT_FACTORY, com.sun.enterprise.naming.SerialInitContextFactory"   );
            ht.put(Context.URL_PKG_PREFIXES, com.sun.enterprise.naming"  );
            ht.put(Context.STATE_FACTORIES, com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl"  );
            ht.put(Context.PROVIDER_URL, "corbaname:iiop:1.2@" + getHostName()+":"+getOrbPort());
            ht.put(ORBManager.DEFAULT_ORB_INIT_HOST, getHostName());
            ht.put(ORBManager.OMG_ORB_INIT_HOST_PROPERTY, getHostName());
            ht.put(ORBManager.OMG_ORB_INIT_PORT_PROPERTY, String.valueOf(getOrbPort()));


Now we do a JNDI lookup to fetch the Queue Connection Factory :


Context ctx = getInitialContext();
Object jms = ctx.lookup(JNDI_JMS_SUB_CONTEXT);
System.out.println("+++ FAST jms ctx : " + jms.toString());
Context jmsSubContext = (Context) jms;
System.out.println("+++ FAST jmsSubContext: " + jmsSubContext.toString());
System.out.println("+++ FAST jmsSubContext class :" + jmsSubContext.lookup(JNDI_CONNECTION_FACTORY_NAME).getClass().getName());

com.sun.messaging.jms.ra.ConnectionFactoryAdapter aJMSConnectionFactory = ( com.sun.messaging.jms.ra.ConnectionFactoryAdapter) jmsSubContext.lookup(JNDI_CONNECTION_FACTORY_NAME);          

if ( !getUserName().equalsIgnoreCase( "" )) {
  // authentication required
   if (getPassword().equalsIgnoreCase( "" ))
     throw new Exception ("Password is NOT set !" );
   else queueConnection = aJMSConnectionFactory.createQueueConnection(getUserName(), getPassword());
}
else {
   queueConnection = aJMSConnectionFactory.createQueueConnection();
}


As a bonus, I also provide an Ant task which can send Text or Bytes messages setting a payload or a file name if you want to send the file content as the payload, see at com.sun.fast.toolbox.jms.ant.task.JmsSenderTask , you can test it using \FAST_JMS_ToolBox\test\com\sun\fast\toolbox\jms\ant\ant-jms.bat or ant-jms.xml (check first the ant-jms.properties)



For further informations, see Ramesh Parthasarathy's Blog entry , see also the Generic JMS RA

[4] Comments
Like this post? del.icio.us | furl | slashdot | technorati | digg
Comments:

hi steve,

can you give your source for download too?

regards chris

Posted by Christian Brennsteiner on juin 26, 2008 at 05:56 PM CEST #

forget my comment i am blind ....

Posted by Christian Brennsteiner on juin 26, 2008 at 05:56 PM CEST #

hi stephe,

i think you should point out what the real difference between the two methods (ra and non ra) is. what implications does it have when i am using the first and what effect will it have to do the second one.

i have a question too. must there any code be written to leverage optimization that is done by embedded jmq in glassfish? or will your ra sample be sufficient?

regards chris

Posted by Christian Brennsteiner on juin 26, 2008 at 06:09 PM CEST #

hi stephe,

i think you should point out what the real difference between the two methods (ra and non ra) is. what implications does it have when i am using the first and what effect will it have to do the second one.

i have a question too. must there any code be written to leverage optimization that is done by embedded jmq in glassfish? or will your ra sample be sufficient?

regards chris

Posted by Christian Brennsteiner on juin 26, 2008 at 06:36 PM CEST #

Post a Comment:
  • HTML Syntax: NOT allowed