Friday, June 17, 2011

Java send JMS: how to send a jms message to WebLogic

package com.acme.jms;


import java.util.Hashtable;

import javax.jms.Message;
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.naming.Context;
import javax.naming.InitialContext;

public class SendJmsMessageHeaderProperties {
    public static void main( String[] args ) throws Exception {
        QueueConnection queueCon = null;
        try {
            // get the initial context, refer to your app server docs for this
            Hashtable<String, String> ht = new Hashtable<String, String>();
            ht.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
            ht.put(Context.PROVIDER_URL, "t3://localhost:7001");       
            Context ctx = new InitialContext(ht);

            // get the connection factory, and open a connection
            QueueConnectionFactory qcf = (QueueConnectionFactory) ctx.lookup( "weblogic/jms/XAConnectionFactory" );
            queueCon = qcf.createQueueConnection();

            // create queue session off the connection
            QueueSession queueSession = queueCon.createQueueSession( false, Session.AUTO_ACKNOWLEDGE );

            // get handle on queue, create a sender and send the message
            Queue queue = (Queue) ctx.lookup( "RudyInQueue" );
            QueueSender sender = queueSession.createSender( queue );

            Message msg = queueSession.createTextMessage( "<Hello>I am a pig</Hello>" );

            sender.send( msg );

            System.out.println( "sent the message" );
        }
        finally {
            // close the queue connection
            if( queueCon != null ) {
                queueCon.close();
            }
        }
    }
}


here is the CLASSPATH:





<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" path="src"/>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6">
        <attributes>
            <attribute name="owner.project.facets" value="java"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
    <classpathentry kind="lib" path="lib/javax.jms_1.1.1.jar"/>
    <classpathentry kind="lib" path="lib/weblogic.jar"/>
    <classpathentry kind="lib" path="lib/com.bea.core.weblogic.security.identity_1.1.2.1.jar"/>
    <classpathentry kind="lib" path="lib/com.bea.core.weblogic.workmanager_1.9.0.0.jar"/>
    <classpathentry kind="lib" path="lib/com.bea.core.weblogic.workmanager.ja_1.9.0.0.jar"/>
    <classpathentry kind="lib" path="lib/com.bea.core.transaction_2.7.0.0.jar"/>
    <classpathentry kind="lib" path="lib/com.bea.core.weblogic.rmi.client.ja_1.8.0.0.jar"/>
    <classpathentry kind="lib" path="lib/com.bea.core.weblogic.rmi.client_1.8.0.0.jar"/>
    <classpathentry kind="lib" path="lib/com.bea.core.weblogic.security.wls_1.0.0.0_6-1-0-0.jar"/>
    <classpathentry kind="lib" path="lib/com.bea.core.utils.full_1.9.0.0.jar"/>
    <classpathentry kind="lib" path="lib/com.bea.core.weblogic.security_1.0.0.0_6-1-0-0.jar"/>
    <classpathentry kind="lib" path="lib/wlclient.jar"/>
    <classpathentry kind="lib" path="lib/com.bea.core.utils.classloaders_1.8.0.0.jar"/>
    <classpathentry kind="lib" path="lib/com.bea.core.management.core_2.8.0.0.jar"/>
    <classpathentry kind="lib" path="lib/com.bea.core.descriptor_1.9.0.0.jar"/>
    <classpathentry kind="lib" path="lib/com.bea.core.logging_1.8.0.0.jar"/>
    <classpathentry kind="lib" path="lib/com.bea.core.timers_1.7.0.0.jar"/>
    <classpathentry kind="lib" path="lib/com.bea.core.weblogic.socket.api_1.2.0.0.jar"/>
    <classpathentry kind="lib" path="lib/com.bea.core.weblogic.security.digest_1.0.0.0.jar"/>
    <classpathentry kind="lib" path="lib/com.bea.core.weblogic.lifecycle_1.4.0.0.jar"/>
    <classpathentry kind="lib" path="lib/com.bea.core.store_1.7.0.0.jar"/>
    <classpathentry kind="lib" path="lib/com.bea.core.common.security.api_1.0.0.0_6-1-0-0.jar"/>
    <classpathentry kind="lib" path="lib/com.bea.core.utils.wrapper_1.4.0.0.jar"/>
    <classpathentry kind="lib" path="lib/com.bea.core.messaging.kernel_1.8.0.0.jar"/>
    <classpathentry kind="lib" path="lib/com.bea.core.utils.expressions_1.4.0.0.jar"/>
    <classpathentry kind="output" path="build/classes"/>
</classpath>






No comments: