Tag: JMS

How to connect to a remote queue in JBoss AS 7 ?

Today one of our subscriber ziggy asked us using comment if we had any article which would show how to connect to a remote queue in JBoss AS7, hence we created this article which would help ziggy as well as others people also. Earlier versions of JBoss AS 7 where not having an inbuilt ability to connect to a queue remotely which was a huge concern given by the community people and users of JBoss. Hence the JBoss team took all the feedback given to them and have come up with a new version JBoss AS 7.1 Final which has an inbuilt ability to connect to a queue remotely and that too with security enabled by default. We personally are really thankful to the whole team members of JBoss AS 7 for putting this feature in the latest version of JBoss AS .

So in this article we will see how can one send messages from Box-2 [i.e. with IP 20.20.20.20] to a queue which is on the Box-1 [i.e. with IP 10.10.10.10] and those messages can be received by the receiver from the same box which is Box-1using a standalone JAVA code for sending and receiving messages. However you can also do vica-versa, which is sending messages to the queue on the same box and receive those messages from a remote box the steps remain same just the standalone JAVA code for sending and receiving messages would switch there places.

Things to remember

  • You should have the “/jboss-as-7.1.0.Final/bin/client/jboss-client-7.1.0.Final.jar” in the CLASSPATH of the client.
  • The URL would be as below, where 4447 is the remote port
  •  remote://<IP_ADDRESS>:4447
    
  • Also you need to create a user for “Application User” using the “/jboss-as-7.1.0.Final/bin/add-user.sh” which would be used for getting the connection with the JBoss on remote box
  • Last thing you would have to add the below code with the username and password created by the above step, for getting the Initial context factory as by default the remote connector is secured.
//*************** Creating Queue Connection using the UserName & Password *************************
qcon = qconFactory.createQueueConnection("USERNAME","PASSWORD");
.
.
//*************** UserName & Password for the Initial Context for JNDI lookup *************************
env.put(Context.SECURITY_PRINCIPAL, "USERNAME");
env.put(Context.SECURITY_CREDENTIALS, "PASSWORD");

In Box-1 [10.10.10.10]

Lets say we have our JBoss AS 7.1 Final in this box and we would be using standalone-full.xml for this article.

  1. Now we would create a queue called TestQ for which we would have to copy the below snip code in standalone-full.xml file inside jms-destinations tag. However you can even create the same queue from the console as well
  2. Console path:

    Profile => (+) Messaging =>  Messaging Provider => default (view ->) => JMS Destination

    Under Destination as Queue click on “Add” Button and enter the following details

    Or else can update the below xml file with the given snip-code

    standalone-full.xml

    .
    .
    <jms-destinations>
                        <jms-queue name="TestQ">
                            <entry name="java:jboss/exported/TestQ"/>
                            <durable>false</durable>
                        </jms-queue>
    .
    .
    

    Note: You would have to make sure you give “java:jboss/exported/” before giving the JNDI name of the queue in the name of “entry” attribute or else you would see the following error

    java QueueSend remote://10.10.10.10:4447
    Mar 3, 2012 6:24:29 PM org.xnio.Xnio
    INFO: XNIO Version 3.0.3.GA
    Mar 3, 2012 6:24:29 PM org.xnio.nio.NioXnio
    INFO: XNIO NIO Implementation Version 3.0.3.GA
    Mar 3, 2012 6:24:29 PM org.jboss.remoting3.EndpointImpl
    INFO: JBoss Remoting version 3.2.2.GA
    Exception in thread "main" javax.naming.NameNotFoundException: TestQ -- service jboss.naming.context.java.jboss.exported.TestQ
    	at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:97)
    	at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:177)
    	at org.jboss.naming.remote.protocol.v1.Protocol$1.handleServerMessage(Protocol.java:124)
    	at org.jboss.naming.remote.protocol.v1.RemoteNamingServerV1$MessageReciever$1.run(RemoteNamingServerV1.java:70)
    	at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    	at java.lang.Thread.run(Thread.java:662)
    
    
  3. Now we need to create a new Application User by running “${JBOSS_HOME}/bin/add-user.sh” script as following :
  4. [userone@localhost bin]$ ./add-user.sh
    
    What type of user do you wish to add?
     a) Management User (mgmt-users.properties)
     b) Application User (application-users.properties)
    (a): b
    
    Enter the details of the new user to add.
    Realm (ApplicationRealm) :  ApplicationRealm
    Username : testuser
    Password : testpassword
    Re-enter Password : testpassword
    
    What roles do you want this user to belong to? (Please enter a comma separated list, or leave blank for none) : testrole
    About to add user 'testuser' for realm 'ApplicationRealm'
    
    Is this correct yes/no? yes
    
    Added user 'testuser' to file '/home/userone/jboss-as-7.1.0.Final/standalone/configuration/application-users.properties'
    Added user 'testuser' to file '/home/userone/jboss-as-7.1.0.Final/domain/configuration/application-users.properties'
    Added user 'testuser' with roles testrole to file '/home/userone/jboss-as-7.1.0.Final/standalone/configuration/application-roles.properties'
    Added user 'testuser' with roles testrole to file '/home/userone/jboss-as-7.1.0.Final/domain/configuration/application-roles.properties'
    

    Updated: Thanks to “michaelbini”

    Note: You would have to make sure you add the new role which we just created “testrole” should be added in the “security-setting” inside “standalone-full.xml” becasue by default all the queues are only having “guest” as role, which has the permission to send and receive messages, however the new user which we made is under “testrole” role and not “guest”, hence we need to add it or else you would see the following error

    Exception in thread "main" javax.jms.JMSSecurityException: User: testuser doesn't have permission='SEND' on address jms.queue.TestQ
    	at org.hornetq.core.protocol.core.impl.ChannelImpl.sendBlocking(ChannelImpl.java:312)
    	at org.hornetq.core.client.impl.ClientProducerImpl.doSend(ClientProducerImpl.java:287)
    	at org.hornetq.core.client.impl.ClientProducerImpl.send(ClientProducerImpl.java:142)
    	at org.hornetq.jms.client.HornetQMessageProducer.doSend(HornetQMessageProducer.java:451)
    	at org.hornetq.jms.client.HornetQMessageProducer.send(HornetQMessageProducer.java:199)
    

    standalone-full.xml:

    Before:

    <security-settings>
      <security-setting match="#">
         <permission type="send" roles="guest"/>
         <permission type="consume" roles="guest"/>
         .
         . 
      </security-setting>
    </security-settings>
    

    After:

    <security-settings>
      <security-setting match="#">
         <permission type="send" roles="guest testrole"/>
         <permission type="consume" roles="guest testrole"/>
         .
         .
      </security-setting>
    </security-settings>
    
  5. Once the queue and application user has been created you can start you JBoss AS 7.1 final by using the following command to let it start using the IP address of the box on which it is running and in this example its 10.10.10.10
  6. bin]$ ./standalone.sh -c standalone-full.xml
     -Djboss.bind.address=10.10.10.10
    
  7. Create a directory somewhere in your file system like: “/urs/JBoss/HornetQ/Queue” to write the QueueReceive.java programs as shown below.
  8. import java.util.Hashtable;
    import javax.jms.*;
    import javax.naming.Context;
    import javax.naming.InitialContext;
    import javax.naming.NamingException;
    
    public class QueueReceive implements MessageListener
    {
    public final static String JNDI_FACTORY="org.jboss.naming.remote.client.InitialContextFactory";
    
    //*************** Using the RemoteConnectionFactory JNDI name *************************
    public final static String JMS_FACTORY="jms/RemoteConnectionFactory";
    
    //*************** Created Queue's JNDI name *************************
    public final static String QUEUE="TestQ";
    
    private QueueConnectionFactory qconFactory;
    private QueueConnection qcon;
    private QueueSession qsession;
    private QueueReceiver qreceiver;
    private Queue queue;
    private boolean quit = false;
    
    public void onMessage(Message msg)
    {
    try {
    String msgText;
    if (msg instanceof TextMessage)
    {
    msgText = ((TextMessage)msg).getText();
    }
    else
    {
    msgText = msg.toString();
    }
    System.out.println("nt "+ msgText );
    if (msgText.equalsIgnoreCase("quit"))
    {
    synchronized(this)
    {
    quit = true;
    this.notifyAll(); // Notify main thread to quit
    }
    }
    }
    catch (JMSException jmse)
    {
    jmse.printStackTrace();
    }
    }
    public void init(Context ctx, String queueName) throws NamingException, JMSException
    {
    qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);
    
    //*************** Creating Queue Connection using the UserName & Password *************************
    qcon = qconFactory.createQueueConnection("testuser","testpassword");			//<------------- Change the UserName & Password
    
    qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
    queue = (Queue) ctx.lookup(queueName);
    qreceiver = qsession.createReceiver(queue);
    qreceiver.setMessageListener(this);
    qcon.start();
    }
    
    public void close()throws JMSException
    {
    qreceiver.close();
    qsession.close();
    qcon.close();
    }
    
    public static void main(String[] args) throws Exception
    {
    if (args.length != 1)
    {
    System.out.println("Usage: java QueueReceive URL");
    return;
    }
    InitialContext ic = getInitialContext(args[0]);
    QueueReceive qr = new QueueReceive();
    qr.init(ic, QUEUE);
    System.out.println("JMS Ready To Receive Messages (To quit, send a "quit" message from QueueSender.class).");
    // Wait until a "quit" message has been received.
    synchronized(qr)
    {
    while (! qr.quit)
    {
    try
    {
    qr.wait();
    }
    catch (InterruptedException ie)
    {}
    }
    }
    qr.close();
    }
    
    private static InitialContext getInitialContext(String url) throws NamingException
    {
    Hashtable env = new Hashtable();
    env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
    env.put(Context.PROVIDER_URL, url);
    
    //*************** UserName & Password for the Initial Context for JNDI lookup *************************
    env.put(Context.SECURITY_PRINCIPAL, "testuser");
    env.put(Context.SECURITY_CREDENTIALS, "testpassword");
    
    return new InitialContext(env);
    }
    }
    

In Box-2 [20.20.20.20]

Now in this box we just need to keep the QueueSend, hence again create a directory somewhere in your file system like: “/urs/JBoss/HornetQ/Queue” to write the QueueSend.java programs as shown below.

QueueSend.java

import java.io.*;
import java.util.Hashtable;
import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class QueueSend
{
public final static String JNDI_FACTORY="org.jboss.naming.remote.client.InitialContextFactory";

//*************** Connection Factory JNDI name *************************
public final static String JMS_FACTORY="jms/RemoteConnectionFactory";

//*************** Queue Factory JNDI name *************************
public final static String QUEUE="TestQ";

private QueueConnectionFactory qconFactory;
private QueueConnection qcon;
private QueueSession qsession;
private QueueSender qsender;
private Queue queue;
private TextMessage msg;

public void init(Context ctx, String queueName)throws NamingException, JMSException
{
qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);

//*************** Creating Queue Connection using the UserName & Password *************************
qcon = qconFactory.createQueueConnection("testuser","testpassword");   			//<------------- Change the UserName & Password

qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
queue = (Queue) ctx.lookup(queueName);
qsender = qsession.createSender(queue);
msg = qsession.createTextMessage();
qcon.start();
}

public void send(String message) throws JMSException {
msg.setText(message);
qsender.send(msg);
}

public void close() throws JMSException {
qsender.close();
qsession.close();
qcon.close();
}

public static void main(String[] args) throws Exception {
if (args.length != 1) {
System.out.println("Usage: java QueueSend URL");
return;
}
InitialContext ic = getInitialContext(args[0]);
QueueSend qs = new QueueSend();
qs.init(ic, QUEUE);
readAndSend(qs);
qs.close();
}

private static void readAndSend(QueueSend qs) throws IOException, JMSException
{
String line="Test Message Body with counter = ";
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
boolean readFlag=true;
System.out.println("ntStart Sending Messages (Enter QUIT to Stop):n");
while(readFlag)
{
System.out.print("<Msg_Sender> ");
String msg=br.readLine();
if(msg.equals("QUIT") || msg.equals("quit"))
{
qs.send(msg);
System.exit(0);
}
qs.send(msg);
System.out.println();
}
br.close();
}

private static InitialContext getInitialContext(String url) throws NamingException
{
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, url);

//*************** UserName & Password for the Initial Context for JNDI lookup *************************
env.put(Context.SECURITY_PRINCIPAL, "testuser");
env.put(Context.SECURITY_CREDENTIALS, "testpassword");

return new InitialContext(env);
}
}

After following the above given steps, we have successfully configured all the setting. Now its time to test our configuration by following the below steps.

Testing

  1. Now open 1– command prompt on each boxes and run the “. ./setJBossEnv.sh” by adding two DOTs separated by a single space, in both the prompts to set the Environment (PATH & CLASSPATH).
  2. setJBossEnv.sh

    #!/bin/sh
    echo "Exporting JBOSS_HOME...."
    export JBOSS_HOME=/Jboss/jboss-as-7.1.0.Final/jboss-as   #<---- Change as per your environment
    echo "JBOSS_HOME Exported....!!!"
    echo ""
    echo "Exporting JAVA_HOME...."
    export JAVA_HOME=/Jdk/jdk1.6.0_21    #<---- Change as per your environment
    echo "JBOSS_HOME Exported....!!!"
    echo ""
    echo "Exporting CLASSPATH...."
    export CLASSPATH=$JBOSS_HOME/bin/client/jboss-client-7.1.0.Final.jar:$CLASSPATH:.:
    echo "CLASSPATH Exported....!!!"
    echo ""
    echo "Exporting PATH...."
    export PATH=$JAVA_HOME/bin:$PATH
    echo "PATH Exported....!!!"
    echo ""
    echo "========================================================================"
    echo "JBOSS_HOME = " $JBOSS_HOME
    echo ""
    echo "JAVA_HOME = " $JAVA_HOME
    echo ""
    echo "CLASSPATH = " $CLASSPATH
    echo ""
    echo "PATH = " $PATH
    echo "========================================================================"
    
  3. Then compile both the java codes the QueueSend.java and QueueReceive.java programs.
  4. Now run the code with the following command
  5. Prompt-1 on Box-1

    java QueueReceive remote://10.10.10.10:4447
     

    Prompt-2 on Box-2

    java QueueSend remote://10.10.10.10:4447
     

Once everything is up and running properly you just have to send around few messages in “Prompt-2” which is on Box-2 and you would notice that those messages would be shown in “Prompt-1” which is running which is on Box-1.

Hence our configuration worked and we are now able to connect to a remote queue in JBoss AS 7.1 Final version, now you can put the sender or receiver to any other box and follow the same steps which would work just fine.

– Should have the “/jboss-as-7.1.0.Final/bin/client/jboss-client-7.1.0.Final.jar” in the CLASSPATH of the client.
– The URL would be   URL =  remote://<IP_ADDRESS>:4447   (i.e. 4447 is the remote port)
– Also you need to create a user for “Application User” using the “/jboss-as-7.1.0.Final/bin/add-user.sh” which would be used for getting the connection with the JBoss on remote box
– IMP = you would have to add the below code with the username and password created by the above step, for getting the Initial context factory as by default the remoting connector is secured.//*************** Creating Queue Connection using the UserName & Password *************************
qcon = qconFactory.createQueueConnection(“USERNAME”,”PASSWORD”); 

//*************** UserName & Password for the Initial Context for JNDI lookup *************************
env.put(Context.SECURITY_PRINCIPAL, “USERNAME”);
env.put(Context.SECURITY_CREDENTIALS, “PASSWORD”);


How to configure EJB3 MDBs on JBoss AS 5.1 for remote IBM Websphere MQ 7?

As promised in our previous article we have come up with the configuration from JBoss AS 5.1 side which would help everyone to consume messages from Websphere MQ 7 which is residing on a remote box. In this article we will see how can one send messages (i.e. using a JAVA standalone code) to a remote queue on Box-1 having Websphere MQ 7 and those messages can be received by the consumer (i.e. using EJB3 MDB deployed on JBoss) from Box-2 which is a very basic scenario in most of the people.

The main concept for this is that we would be sending messages to the remote queue of Websphere MQ 7 on box-1 and on box-2 we would be creating a Websphere MQ provider which would have the URL and InitialContextFactory of the Websphere MQ having the queue on box-1 and using that Websphere MQ provider we would connect to the connection factory and would connect to the queue for Websphere MQ which is on box-1. Once we get the access of connection factory and queue we would just create an MDB which would using the Websphere MQ provider details and would be able to consume message from box-2 easily.

In Box-1 (10.10.10.10)

Lets say you have your Websphere MQ 7 running in this box and you can configure the remote queue in MQ 7 by following the steps given in the below article

Steps to Create A Remote Queue in WebSphere MQ 7

In Box-2 (20.20.20.20)

In this box we have a JBoss node called Node-1 and following things has to be done on this box.

Note: You would need the following jar files from Websphere MQ side to put in the /Node-1/lib directory

  • dhbcore.jar
  • mqcontext.jar
  • com.ibm.mq.jar
  • com.ibm.mqjms.jar
  • com.ibm.mq.pcf.jar
  • com.ibm.mq.jmqi.jar
  • com.ibm.mq.headers.jar
  • com.ibm.mq.commonservices.jar

.

  1. Put the “wsIBMmq-provider-service.xml” file in the “Node-1/deploy” folder
  2. wsIBMmq-provider-service.xml

    <server>
     <mbean code="org.jboss.jms.jndi.JMSProviderLoader" name="jboss.jms:service=JMSProviderLoader,name=WSIBMMQJMSProvider">
     <attribute name="ProviderName">WSIBMMQJMSProvider</attribute>
     <attribute name="ProviderAdapterClass">org.jboss.jms.jndi.JNDIProviderAdapter</attribute>
    <!-- Connection Factories name created on MQ, "JMS Administered Objects => <IP_ADDRESS>:<PORT>/<CHANNELS_NAME> => Connection Factoiries => <CONNECTION_FACTOIRIES_NAME> "-->
     <attribute name="FactoryRef">MyRemoteMQMgr</attribute>
     <attribute name="QueueFactoryRef">MyRemoteMQMgr</attribute>
     <attribute name="TopicFactoryRef">MyRemoteMQMgr</attribute>
     <attribute name="Properties">
          java.naming.factory.initial=com.ibm.mq.jms.context.WMQInitialContextFactory
          java.naming.provider.url=10.10.10.10:1415/MyRemoteChannel
          java.naming.security.authentication=none
     </attribute>
     </mbean>
     </server>
    

    Note: In “wsIBMmq-provider-service.xml” file you have to change the 10.10.10.10 with the your remote server name/IP address of the remote server on which MQ 7 is running and same goes for 1415 and MyRemoteChannel.

  3. Put the “MDB_30.ear” files in the “Node-1/deploy” folder
    1. Create a folder called “MDB_30.ear
    2. Under it create 2 folders called “MDB_30.jar” and “META-INF
    3. Inside “META-INF” create a “application.xml” file and update this file with the below details
    4. <?xml version="1.0" encoding="UTF-8"?>
      <application version="5" xmlns="http://java.sun.com/xml/ns/javaee"
                    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                    http://java.sun.com/xml/ns/javaee/application_5.xsd">
         <module>
             <ejb>MDB_30.jar</ejb>
         </module>
      </application>
      
    5. Inside “MDB_30.jar” create a “MyMDB.java” file and update this file with the below details
    6. import javax.ejb.MessageDriven;
      import javax.jms.JMSException;
      import javax.jms.Message;
      import javax.jms.MessageListener;
      import javax.jms.TextMessage;
      import javax.ejb.ActivationConfigProperty;
      
      @MessageDriven(
          activationConfig={
              @ActivationConfigProperty(propertyName="providerAdapterJNDI", propertyValue="WSIBMMQJMSProvider"),
              @ActivationConfigProperty(propertyName="destinationType", propertyValue="javax.jms.Queue"),
              @ActivationConfigProperty(propertyName="destination", propertyValue="MyRemoteQueue"),  
              @ActivationConfigProperty(propertyName="useDLQ", propertyValue="false"),
      	@ActivationConfigProperty(propertyName="reconnectAttempts", propertyValue="60"),
      	@ActivationConfigProperty(propertyName="reconnectInterval", propertyValue="10")
          }
      )
      
      public class MyMDB implements MessageListener{
      
          public void onMessage(Message message) {
              TextMessage textMessage = (TextMessage) message;
              try {
                  System.out.println("nnt Message Received by MDB : "+ textMessage.getText());
              } catch (JMSException e) {
                  e.printStackTrace();
              }
          }
      }
      
    7. Once every thing has been put at its place, then you compile the MyMDB.java code and if you want then you can also create an EAR archive file for above folder
  4. After following the above given steps, we have successfully configured all the setting from JBoss end. Now its time to test our configuration by following the below steps.

    Testing

    1. Create a Directory somewhere in your file system like: “/urs/JBoss/JBM/Queue” to write the QueueSend.java and as we have an MDB we would not need a QueueReceive.java programs.
    2. In QueueSend.java copy the below program
      import java.io.*;
      import java.io.*;
      import java.util.Hashtable;
      import javax.jms.JMSException;
      import javax.jms.Queue;
      import javax.jms.QueueConnection;
      import javax.jms.QueueConnectionFactory;
      import javax.jms.QueueSender;
      import javax.jms.QueueSession;
      import javax.jms.Session;
      import javax.jms.TextMessage;
      import javax.naming.Context;
      import javax.naming.InitialContext;
      import javax.naming.NamingException;
      
      public class QueueSend
      {
      public final static String JNDI_FACTORY="com.ibm.mq.jms.context.WMQInitialContextFactory";
      
      //*************** Connection Factory JNDI name *************************
      public final static String JMS_FACTORY="MyRemoteMQMgr"; 
      
      //*************** Queue Factory JNDI name *************************
      public final static String QUEUE="MyRemoteQueue";
      
      
      private QueueConnectionFactory qconFactory;
      private QueueConnection qcon;
      private QueueSession qsession;
      private QueueSender qsender;
      private Queue queue;
      private TextMessage msg;
      
      public void init(Context ctx, String queueName)throws NamingException, JMSException
      {
      qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);
      qcon = qconFactory.createQueueConnection();
      qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
      queue = (Queue) ctx.lookup(queueName);
      qsender = qsession.createSender(queue);
      msg = qsession.createTextMessage();
      qcon.start();
      }
      
      public void send(String message) throws JMSException {
      msg.setText(message);
      qsender.send(msg);
      }
      
      public void close() throws JMSException {
      qsender.close();
      qsession.close();
      qcon.close();
      }
      
      public static void main(String[] args) throws Exception {
      if (args.length != 1) {
      System.out.println("Usage: java QueueSend URL");
      return;
      }
      InitialContext ic = getInitialContext(args[0]);
      QueueSend qs = new QueueSend();
      qs.init(ic, QUEUE);
      readAndSend(qs);
      qs.close();
      }
      
      private static void readAndSend(QueueSend qs) throws IOException, JMSException
      {
      String line="Test Message Body with counter = ";
      BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
      boolean readFlag=true;
      System.out.println("ntStart Sending Messages (Enter QUIT to Stop):n");
      while(readFlag)
      {
      System.out.print("<Msg_Sender> ");
      String msg=br.readLine();
      if(msg.equals("QUIT") || msg.equals("quit"))
      {
      qs.send(msg);
      System.exit(0);
      }
      qs.send(msg);
      System.out.println();
      }
      br.close();
      }
      
      private static InitialContext getInitialContext(String url) throws NamingException
      {
      Hashtable env = new Hashtable();
      env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
      env.put(Context.PROVIDER_URL, url);
      return new InitialContext(env);
      }
      }
      
    3. Now open just 1 – command prompt and run the “. ./setMQ_CLASSPATH.sh” by adding two DOTs separated by a single space, in both the prompts to set the Environment (PATH & CLASSPATH).
    4. Note: The same jar files shown above would be required here as well, hence change the path given in the below script under MQ_LIB

      setMQ_CLASSPATH.sh

      #!/bin/sh
      
      echo "Exporting CLASSPATH for IBM MQ...."
      
      MQ_LIB=/home/urs/Remote_MQ/lib
      
      export CLASSPATH=$CLASSPATH:$MQ_LIB/com.ibm.mq.commonservices.jar:$MQ_LIB/com.ibm.mq.headers.jar:$MQ_LIB/com.ibm.mq.jmqi.jar:$MQ_LIB/com.ibm.mq.pcf.jar:$MQ_LIB/com.ibm.mqjms.jar:$MQ_LIB/dhbcore.jar:$MQ_LIB/mqcontext.jar:$MQ_LIB/com.ibm.mq.jar:.:
      echo "====================================="
      echo "CLASSPATH =" $CLASSPATH
      echo "====================================="
      
    5. Then compile the QueueSend.java programs.
    6. Now run the code with the following command
    7. Prompt-1

      java QueueSend 10.10.10.10:1415/MyRemoteChannel
       

    Once everything is up and running properly you just have to send around few messages in “Prompt-1” and you would notice that those messages would be shown in shell on which the Node-1 is running which is on Box-2. Hence when you are sending a messages to the Websphere MQ 7 queue which is on Box-1 the same messages are been consumed by the MDB which is been deployed on Node-1 running on Box-2.


Steps to Create A Remote Queue in WebSphere MQ 7

In this article we would be sharing just the steps to create a queue in WebSphere MQ 7 which can be used with JBoss to send or receive message from a remote box which would be in our next article. Now normally everyone has a separate box on which the WebSphere MQ 7 is been installed and another box on which JBoss is running, hence in this scenario things get little different as the queue which has to be created should be accessed by a remote box where lot of people get stuck and we were one of them too.

As we were not from a background of WebSphere MQ, it was difficult for us to configure the queue and other configuration properly so that it can be accessed by a remote server. After lot of research and struggle we were able to get the configuration correct and working hence we would like to share our findings with you as always.

Following are the details from where you can register yourself to download WebSphere MQ 7, installation of it and the complete steps to create a remote queue in WebSphere MQ 7 which would be accessible from a remote server.

Download Link:

You can download “Evaluate: IBM WebSphere MQ” version V7.1 from the below link, however if you do not have an account opened yet you can register yourself from the link

Download LinkEvaluate: IBM WebSphere MQ version V7.1

Registration Link My IBM registration

Installation Steps:

You can follow the steps given in the below given IBM docs link which would share you all the methods of installation

Installation Steps LinkWebSphere MQ Server installation methods

Steps to Create A Remote Queue in WebSphere MQ 7:

Here are the complete steps which would help you creating a remote queue in WebSphere MQ 7 with screen-shorts for your convince hope it would help.

Creating a Queue Manager:

  1. Right-click on “Queue Managers” and select “New” -> “Queue Manager
  2. Enter the name for “Queue manager name:” and click 3 times Next, however you can change the default setting if you feel like.
  3. You can change the “Listen on port number” if you have other Queue Manger running on default port which is 1414 then click Next and Finish.

Creating a Queue:

  1. Right-click on “Queues” under the Queue Manager which we created and select “New” -> “Local Queue
  2. Enter the name for Queue “Name” and click “Next” and then “Finish“, however you can change the default setting if you feel like.

Creating a Channels:

  1. In your Queue Manager, click the (+) for “Advanced” and then right-click on the “Channels” and select “New” -> “Server-connection Channel
  2. Enter the name for the Channel “Name” and click “Next
  3. Now you would have to make sure that you have given the correct value for “MCA user ID“, if you have kept it blank then you would see the below error when a client would try to look up.
  4. Remote_MQ]$ java QueueReceive 10.10.10.10:1415/MyRemoteChannel
    Exception in thread "main" javax.naming.NamingException: Unable to connect to the admin queue. Exception was: com.ibm.msg.client.jms.DetailedJMSSecurityException: JMSWMQ2013: The security authentication was not valid that was supplied for QueueManager 'MyRemoteMQMgr' with connection mode 'Client' and host name '10.65.211.223'. Please check if the supplied username and password are correct on the QueueManager you are connecting to [Root exception is com.ibm.msg.client.jms.DetailedJMSSecurityException: JMSWMQ2013: The security authentication was not valid that was supplied for QueueManager 'MyRemoteMQMgr' with connection mode 'Client' and host name '10.65.211.223'. Please check if the supplied username and password are correct on the QueueManager you are connecting to]
    	at com.ibm.mq.jms.context.MQContext.<init>(MQContext.java:318)
    	at com.ibm.mq.jms.context.WMQInitialContextFactory.getInitialContext(WMQInitialContextFactory.java:29)
    	at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:667)
    	at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
    	at javax.naming.InitialContext.init(InitialContext.java:223)
    	at javax.naming.InitialContext.<init>(InitialContext.java:197)
    	at QueueReceive.getInitialContext(QueueReceive.java:102)
    	at QueueReceive.main(QueueReceive.java:77)
    Caused by: com.ibm.msg.client.jms.DetailedJMSSecurityException: JMSWMQ2013: The security authentication was not valid that was supplied for QueueManager 'MyRemoteMQMgr' with connection mode 'Client' and host name '10.65.211.223'. Please check if the supplied username and password are correct on the QueueManager you are connecting to
    	at com.ibm.msg.client.wmq.common.internal.Reason.reasonToException(Reason.java:531)
    	at com.ibm.msg.client.wmq.common.internal.Reason.createException(Reason.java:219)
    	at com.ibm.msg.client.wmq.internal.WMQConnection.<init>(WMQConnection.java:410)
    	at com.ibm.msg.client.wmq.factories.WMQConnectionFactory.createV7ProviderConnection(WMQConnectionFactory.java:7855)
    	at com.ibm.msg.client.wmq.factories.WMQConnectionFactory.createProviderConnection(WMQConnectionFactory.java:7331)
    	at com.ibm.msg.client.jms.admin.JmsConnectionFactoryImpl.createConnection(JmsConnectionFactoryImpl.java:276)
    	at com.ibm.mq.jms.MQConnectionFactory.createCommonConnection(MQConnectionFactory.java:6055)
    	at com.ibm.mq.jms.MQQueueConnectionFactory.createQueueConnection(MQQueueConnectionFactory.java:144)
    	at com.ibm.mq.jms.context.MQContext.<init>(MQContext.java:311)
    	... 7 more
    Caused by: com.ibm.mq.MQException: JMSCMQ0001: WebSphere MQ call failed with compcode '2' ('MQCC_FAILED') reason '2035' ('MQRC_NOT_AUTHORIZED').
    	at com.ibm.msg.client.wmq.common.internal.Reason.createException(Reason.java:206)
    
  5. You can also make the “SSL” as “Optional” and click OK

Creating JMS Administered Objects:

  1. In your Queue Manager and then right-click on the “JMS Administered Obejects” and select “Add Initial Context
  2. Now select the

    1. Radio button for “Other”
    2. Factory class = com.ibm.mq.jms.context.WMQInitialContextFactory
    3. Required libraries=//mqcontext.jar
    4. Provider URL = :/
    5. Click 3 times “Next” and then “Finish“, however you can change the default setting if you feel like.
  3. Once this is done you would see “10.10.10.10:1415/MyRemoteChannel” is “Connected” and “MyRemoteChannel” created would also be “Running” now.
  4. Now you would have to use the connection factories name given in the “10.10.10.10:1415/MyRemoteChannel” => Connection Factories => Name (i.e. it would be similar name of your Queue Manager)

This way you would be able to create a remote queue in WebSphere MQ 7 and in our next article we would be sharing the code on how to connect this queue with JBoss, so stay tuned…


Copyright © 2010-2012 Middleware Magic. All rights reserved. |