WebLogic and JMS case configuration

Source: Internet
Author: User
Tags qcon

1. Configuring the JMS server in WEBLOGIC12

Reference Link: http://blog.csdn.net/gxlstone/article/details/41378949

2. Create Java project to write test code

(1) Create a Java project (not a Web project)

(2) Add the Wlclient.jar in the Weblogic installation directory to the CLASSPATH (note: Weblogic old version of the words use Weblogic.jar), specific path: $Weblogic _home\wlserver_10.3\ Server\lib.

(3) Javaee.jar should be added to Classpath because we are building a Java Project project and will not automatically add Java EE packages. Note: Download the Java EE SDK from the official website and have this package in Lib after installation.

Package Com.jms;import java.util.Hashtable;  Import javax.jms.JMSException;  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.jms.textmessage;import javax.naming.Context;  Import Javax.naming.InitialContext;  Import javax.naming.NamingException; Producer   public class Jmssender {public static final String jndi_factory = "Weblogic.jndi.WLInitialContextFactory";p Rivate static queueconnectionfactory qconfactory;private static queueconnection qcon;private static queuesession Qsession;private static Queuesender qsender;private static Queue queue;private static textmessage msg;public static void I NIT () throws Namingexception, JMSException {//init JNDI context String jndifactory = "Weblogic.jndi.WLInitialC Ontextfactory ";//define JNDI context factory String Providerurl =" t3://localhost:7001 ";          Define WebLogic JMS URL Hashtable env = new Hashtable ();          Env.put (Context.initial_context_factory, jndifactory);          Env.put (Context.provider_url, Providerurl);          Context CTX = new InitialContext (env); String queuename = "Sevenqueue";//Generator queue String Connfactoryjndi = "myjmsconnectionfactory";//factory//Get a JMS connection factory to get a service Connection to the device. The previous question is that this JMS factory must be created beforehand, note: The query is the name of the Jndi of the connection factory, not the name of the connection factory. Qconfactory = ((queueconnectionfactory) ctx.lookup (Connfactoryjndi));//Create a queue or topic connection in order to get session Qcon with the JMS session Qconfactory.createqueueconnection ();//The session is created to create a message sender and send content type. Qsession = Qcon.createqueuesession (false, Session.auto_acknowledge);//Create a queue to store the message, which is the physical address of the sender of the message. The previous question is that this queue needs to be created in WebLogic. Queue = ((queue) Ctx.lookup (queuename));///Create a message sender Qsender = qsession.createsender (queue);//Create a Send content type (Stream, Byte,map,text,object). msg = Qsession.createtextmessage ();//A String object//Open connection, set send text content. Qcon.start (); ctx = null;} public static void Send (String message) throws JMSException {msg.settext (message);//Set Send content Qsender.send (msg);} public static void Close () throws JMSException {if (qsender! = null) {Qsender.close ();} if (qsession! = null) {Qsession.close ();}    if (Qcon! = null) Qcon.close ();} /** * @param args * @throws namingexception * @throws jmsexception */public static void main (S       Tring[] args) throws Namingexception, jmsexception {init ();       Send ("haha");    Close ();   }    }

Package Com.jms;import Java.util.hashtable;import Java.util.map;import javax.jms.BytesMessage;  Import javax.jms.JMSException;  Import Javax.jms.MapMessage;  Import Javax.jms.Message;  Import Javax.jms.MessageListener;  Import Javax.jms.ObjectMessage;  Import Javax.jms.Queue;  Import javax.jms.QueueConnection;  Import Javax.jms.QueueConnectionFactory;  Import Javax.jms.QueueReceiver;  Import javax.jms.QueueSession;  Import javax.jms.Session;  Import Javax.jms.StreamMessage;  Import Javax.jms.TextMessage;  Import Javax.naming.Context;  Import Javax.naming.InitialContext;    Import Javax.naming.namingexception;import org.dom4j.element;import com.utils.base64;import com.utils.XmlHelper;             Consumer public class Jmsreceiver {public static void main (string[] args) throws Namingexception, JMSException { Init jndi context String jndifactory = "Weblogic.jndi.WLInitialContextFactory";//define Jndi context FAC Tory String Providerurl = "t3://localhost:7001"; DeFine weblogic JMS URL Hashtable env = new Hashtable ();          Env.put (Context.initial_context_factory, jndifactory);          Env.put (Context.provider_url, Providerurl);                    Context CTX = new InitialContext (env); Find Connection factory String Connfactoryjndi = "Myjmsconnectionfactory"; JMS connectionfactory JNDI name queueconnectionfactory connfactory = (queueconnectionfactory) ctx.lookup (connFa          Ctoryjndi);          Create queue connection Queueconnection Qconn = (queueconnection) connfactory.createconnection ();          Create session Queuesession Qsession = Qconn.createqueuesession (false, Session.auto_acknowledge);                    Find queue by JNDI Lookup Queue queue = (queue) ctx.lookup ("Sevenqueue");          Create receiver QueueReceiver qreceiver = qsession.createreceiver (queue);  Create Message Listener Qreceiver.setmessagelistener (new MessageListener () {              public void OnMessage (Message msg) {String msgtext = "";                   Double d = 0; try {if (msg instanceof textmessage) {Msgtext = ((textmessage) msg)                          . GetText ();                                 } else if (msg instanceof streammessage) {Msgtext = ((streammessage) msg). ReadString ();                             D = ((Streammessage) msg). Readdouble ();                                 } else if (msg instanceof bytesmessage) {byte[] block = new byte[1024];                                 ((bytesmessage) msg). Readbytes (block);                             Msgtext = string.valueof (block);                             } else if (msg instanceof mapmessage) {Msgtext = ((mapmessage) msg). GetString ("name"); }} catch (Jmsexception e) {//TODO auto-generated catch block E.printstacktrace ();              } System.out.println (Msgtext + "" + D);            }});                Qconn.start ();   }        }

  

WebLogic and JMS case configuration

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.