Loading
Websphere Message Queue Example .

Java Quick Notes

Refresh Your Java - Before Java Interview

We are Agile, believe in less Documentation - Only Quick notes (Java Interview Questions) of Java/J2ee Read more....


Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet

Go to SiteMap

Q)  Websphere Message Queue Example .


Ans)

import java.util.Properties;

import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.naming.Context;
import javax.naming.InitialContext;

public class TestMQ {

        public static void main(String[] args) {
                try {
                        Properties p = new Properties();
                    p.put("org.omg.CORBA.ORBClass","com.sun.CORBA.iiop.ORB");
                    //p.put("org.omg.CORBA.ORBSingletonClass","com.ibm.rmi.corba.ORBSingleton");
                   // p.put("javax.rmi.CORBA.UtilClass","com.ibm.rmi.javax.rmi.CORBA.Util");
                  //  p.put("javax.rmi.CORBA.StubClass","com.ibm.rmi.javax.rmi.CORBA.StubDelegateImpl");
                   // p.put("java.naming.factory.url.pkgs","com.ibm.ws.naming");

                        p.setProperty( javax.naming.Context.INITIAL_CONTEXT_FACTORY,
                        "com.ibm.websphere.naming.WsnInitialContextFactory" );
                        //javax.naming.Context.INITIAL_CONTEXT_FACTORY, for the property name,
                        //and the constant, com.ibm.websphere.naming.PROPS.INITIAL_CONTEXT_FACTORY, for the property value.

                        p.setProperty( "java.naming.provider.url", "iiop://HOSTNAME:PORT" );
                      
                        String connectionfactoryjndi="jms/testQCF";
                        //String sendQueueName="jms/apinboundq";
                        String sendQueueName="jms/destOutboundQ";
              
                        Context jndiContext = new InitialContext();
                        jndiContext = new InitialContext( p );

                        QueueConnectionFactory cf = (QueueConnectionFactory) jndiContext.lookup(connectionfactoryjndi);
                        System.out.println("Looked up : QueueConnectionFactory "+ connectionfactoryjndi);
                        System.out.println("Transport Queue [" + sendQueueName + "]");

                        QueueConnection connection = (QueueConnection) cf.createQueueConnection();
                        QueueSession session = (QueueSession) connection.createQueueSession(false, Session.CLIENT_ACKNOWLEDGE);

                        Queue queue = (Queue) jndiContext.lookup(sendQueueName);
                        QueueSender sender =  (QueueSender) session.createSender(queue);
                        QueueReceiver receiver = (QueueReceiver) session.createReceiver(queue);   

                        long uniqueNumber = System.currentTimeMillis() % 1000;
                        javax.jms.TextMessage message =  session.createTextMessage("Test Message "+ uniqueNumber);  

                        // Start the connection
                        connection.start();

                        sender.send(message);
                //      System.out.println("Sent message:\\n" + message);

 
                        sender.close();
                        receiver.close();
                        session.close();
                        connection.close();

                        System.out.println("\\nSUCCESS\\n");
                }
                catch (JMSException jmsex) {
                        System.out.println(jmsex);
                        System.out.println("\\nFAILURE\\n");
                }
                catch (Exception ex) {
                        System.out.println(ex);
                        System.out.println("\\nFAILURE\\n");
                }
        }
}



Back to top

------------------------- We hope you got necessary Info On -----------------------------------------

Websphere Message Queue Example .


Websphere Message Queue Example .

-------------------------------------------------------------------------------------------------------



Face Book
Request for a Mock Interview/Training

Get a PDF

Face Book
Same look (Read) on any device, this is Ads free