Wednesday, March 16, 2011

Me, JMS, HermesJMS and JBoss

http://www.hermesjms.com/


excellent movie here on how to use HermesJMS to connect to JBossMQ:
http://www.hermesjms.com/demos/jboss_config.html

in practice:
configuration
providers
add group name
call it "JBoss 4.0.1"

add jars:

jboss-client, jbossall-client, jbossmq-client, jmx-invoker-adaptor-client, jnp-client (they are in C:\acme\dev\jboss\jboss-4.0.3SP1\client)
concurrent, jboss-jmx
(they are in C:\acme\dev\jboss\jboss-4.0.3SP1\lib)

create initial context

loader=JBoss 4.0.1
providerURL=jnp://localhost:1099
initialContextfactory=org.jnp.interfaces.NamingContextFactory
urlPackagePrefixes=org.jnp.interfaces:org.jboss.naming
securityCredentials=admin
securityAuthentication=admin


here more info on which parameters to use to create a connection:

http://oatv.com/pub/a/onjava/2006/02/22/asynchronous-messaging-with-spring-jms.html?page=3


and at page 5 some Java code on how to send messages to JBossMQ


For convenience, here is the working code:

 private void sendJMS() {
  try {
   String JNDINAME = "queue/testQueue";
      String queueName = "queue/testQueue";
      System.out.println("Queue name is " + queueName);

      InitialContext jndiContext = null;
   /*
       * Create JNDI Initial Context
       */
      try {
          Hashtable env = new Hashtable();
          env.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
          env.put("java.naming.provider.url","localhost");
          env.put("java.naming.factory.url.pkgs",
              "org.jnp.interfaces:org.jboss.naming");

          jndiContext = new InitialContext(env);
      } catch (NamingException e) {
          System.out.println("Could not create JNDI API " +
              "context: " + e.toString());
      }

      QueueConnectionFactory queueConnectionFactory = null;
   Queue queue = null;
   /*
       * Get queue connection factory and queue objects from JNDI context.
       */
      try {
          queueConnectionFactory = (QueueConnectionFactory)
          jndiContext.lookup("UIL2ConnectionFactory");

          queue = (Queue) jndiContext.lookup(queueName);
      } catch (NamingException e) {
          System.out.println("JNDI API lookup failed: " + e.toString());
      }

      QueueConnection queueConnection = null;
   /*
       * Create connection, session, sender objects.
       * Send the message.
       * Cleanup JMS connection.
       */
      try {
          queueConnection =
              queueConnectionFactory.createQueueConnection();
          QueueSession queueSession = queueConnection.createQueueSession(false,
                  Session.AUTO_ACKNOWLEDGE);
          QueueSender queueSender = queueSession.createSender(queue);
          TextMessage message = queueSession.createTextMessage();
          message.setText("This is a sample JMS message.");
          System.out.println("Sending message: " + message.getText());
          queueSender.send(message);

      } catch (JMSException e) {
       logger.error("Exception occurred: ", e);
          
      } finally {
          if (queueConnection != null) {
              try {
                  queueConnection.close();
              } catch (JMSException e) {
               logger.error("unable to close queueConnection", e);
              }
          }
      }
  }
  catch (Exception e) {
   logger.error("unable to send JMS", e);
  }

 }



No comments: