BEA WebLogic Server8.1 JMS Getting Started

xiaoxiao2021-03-06  51

I. Introduction to JMS 1. JMS Basic Concept JMS (Java Message Service) is a standard API to access the enterprise message system, which is easy to exchange the Java application in the message system for message exchange, and the interface is transmitted, transmitted, and receives the message. Simplify the development of enterprise applications. 2. JMS Basic Features JMS is an application interface for communication with a message-oriented middleware. It supports the domain of point-to-point, but also supports publish / subscribe (publish / subscribe) types, and provides support for the following types: approved messaging, transaction messaging, consistency Messages and subscribers with persistence. JMS also provides another way to integrate your application with the old background system. 3. WebLogic JMS Server Introduction WebLogic Server8.1 complies with the Java specification and passes Sun Microsystems J2EE 1.3 authentication. As part of WebLogic, of course WebLogic JMS Server fully complies with JMS specification, but also supports clusters, and can be applied to actual enterprise systems. The figure is a WebLogic JMS Server architecture. You can see that WebLogic JMS Server components are: WebLogic JMS Servers, Java client, JNDI (for domain name lookup), backup storage (for persistent message storage) , Based on file or JDBC database). II. WebLogic JMS feature 1. Message communication model JMS supports two messaging models: Point-to-Point (PTP) model and publish / subscription (Pub / Sub) model . In addition to the following differences, the two message communication models are very similar: the PTP model specifies that a message can only have a recipient; the PUB / SUB model allows a message to have multiple recipients. 2. The center of the message is the center of the message delivery system is the message. A Message is divided into three components: Header is a standard field set, client and supplier use it to identify and routing messages. · Property supports adds the optional header field to the message. If your application needs to categorize the message and category without using the Standard Header field, you can add an attribute to the message to implement this catalog and classification. SET

Property (...) and get

Property (...) method to set and get properties of various Java types, including Object. JMS defines a set of standard properties provided by suppliers.

• The main body of the message contains the content to be sent to the receiving application. Each message interface is specific to the type of content it supports.

JMS provides their respective message types for different types of content, but all messages are derived from the Message interface.

· StreamMessage: Contains the basic numerical flow of Java, fill and read in order with standard flow operations.

· MapMessage: Contains a set of names / value pairs; named String type, while the value is the basic type of Java.

· TextMessage: Contains a string.

· ObjectMessage: Contains a Serializable Java object; you can use JDK collection classes.

· BYTESMESSAGE: Contains uninterrupted word stream: encoding the body to match the existing message format.

• XMLMessage: Contains XML content. The use of TextMessage, XMLMessage types makes message filtration very convenient.

3. Message confirmation mode

In a non-transactional session, the session created by the application has five confirmation patterns, and in the transactional session, the confirmation mode is ignored.

Five confirmation mode description:

· Auto_acknowledge: Automatic confirmation mode. Once the receiver application is returned from the processing message, the session object will confirm the reception of the message. · Client_acknowledge: Client confirmation mode. The session object relies on an application to call an Acknowledge () method for the received message. Once this method is called, the session confirms all received messages after the last confirmation. This mode allows applications to receive, process and confirm a batch of messages in a call. Note: In the management console, if the Acknowledge POLICY (Confirmation Policy) attribute of the connection factory is set to "Previous", you want to confirm all received messages for a given session, then use the last message To call the Acknowledge () method.

· DUPS_OK_ACKNOWEDGE: Allows the copy of the copy. Once the receiver application is returned from the processing message, the session object will confirm the reception of the message; and allow repeated confirmation. This mode is very effective when you need to consider the use of resources. Note: If your application cannot handle duplicate messages, you should avoid using this mode. If the initialization attempt to send messages fails, the repeated message can be resended.

· No_acknowledge: Unsave mode. Not confirming that the received message is needed. When the message is sent to a NO_ACKNOWLEDGE session, they will be deleted immediately by the WebLogic server. In this mode, the received message will not be re-obtained, and may result in the following result: 1. Message may be lost; and (or) another situation: 2. If the initialization attempt to send the message failed, repetition The message is sent.

· MULTICAST_NO_ACKNOWEDGE: Uncertainwords under IP multicast, there is also no confirmation. Send a message to a multicast_no_acknowledge session, which is the same characteristic as the NO_ACKNOWLEDGE confirmation mode described above. This mode supports applications that wish to communicate through IP multicast methods, without relying on the quality of service provided by session confirmation. Note: If your application cannot process the loss or repetition of the message, you should avoid using this mode. If the initialization attempt to send a message failed, the duplicate message may be sent again.

Note: AUTO_AKNOWLEDGE, DUPS_OK_ACKNOWLEDGE and

Client_acknowledge is a JMS specification definition, no_acknowledge and multicast_no_acknowledge are provided by WebLogic JMS.

3. Configuring JMS

Create a connection plant

(1) Start WebLogic Server8.1, log in to the console, select the JMS Connection Factories node, click on "Configure a new JMS Connection Factory ..." on the right;

(2) Fill in the name of the connection plant SendjmsFactory and JNDi name SendjmsFactory, click "CREATE";

(3) Hook "MyServer" and apply SendJMsFactory to MyServer;

2. Define the backup storage

(1) Select the JMS Stores node, click on "Configure a New JMS Connection Factory ..." on the right.

(2) Fill in the name of the file backup SENDFILESTORE and directory Directionary E: / bea / user_projects / domains / mydomain / sendfilestore, click "Create" .3. Create JMS Server

(1) Select the JMS Servers node and click on "Configure a new jmsserver ..." on the right;

(2) Fill in the name of the JMS server Sendjmsserver and Paging Store set to "SendFilestore", click "Create";

(3) Target Select "MyServer" and apply SendJMSServer to MyServer.

4. Create a message queue

(1) Expand the "SendJmsserver" node, click "Configure a new jms queue ...";

(2) Fill in the name of the message queue Sendjmsqueue and JNDI name Sendjmsqueue, click "CREATE";

IV. JMS application

A JMS application consists of the following elements:

· JMS client. Send and receive the Java program for messages with JMS API.

· Non-JMS (Non-JMS) client. Recognizing this is important - old procedures are often part of the entire JMS application, and their containment should be pre-considered in design.

News. The format and content of the messages exchanged between JMS and non-JMS clients are parts that must be considered by the JMS application design.

· JMS supplier. Suppliers must provide specific implementations specific to their MOM products.

· Managed objects. Message delivery system supplier administrators created an object that is independent of supplier proprietary technology. Includes connection factories ConnectionFactory and destination Destination.

A typical JMS program requires the following steps to start messaging and use:

· Find ConnectionFactory through JNDI.

· Find one or more destination via JNDI.

• Create a Connection with ConnectionFactory.

· Create one or more session with Connection.

• Create the required MessageProducer and MessageConsumer with Session and Destination.

· Start Connection.

The process sent and received by the JMS resource presentation point is used below.

V. Design Message Send

1. JMS resources used

Server URL: T3: // Localhost: 80

Connecting factory: SendjmsFactory

Queue: Sendjmsqueue

2. Design steps

· Initialization JNDI TREE

Hashtable env = new hashtable ();

Env.put (Context.Initial_Context_Factory, JNDi_Factory);

Env.put (Context.Provider_URL, Provider_URL);

Return New InitialContext (ENV);

· Lookup ConnectionFactory

QCONFACTORY = (QueueConnectionFactory) CTX.lookup (JMS_Factory);

· Lookup Destination

Queue = (queue) ctx.lookup (queuename);

· Create Connection with ConnectionFactory

QCON = qConFactory.createqueueConnection ();

· Create a session with Connection

Qsession = QCREATEQUEESESSSION (FALSE, SESSION.AUTO_ACKNOWEDGE); • Create MessageProducer with session and Destination

Qsender = Qsession.createsender (queue);

· Start Connection.

QCON.START ();

· send messages

Msg = Qsession.createTextMessage ();

Msg.Settext (Message);

QSENDER.SEND (MSG);

3. Source code

Package JMssample;

import java.util.Hashtable; import javax.jms *;. import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import java.io.BufferedReader; import java.io.IOException; import Java.io.inputstreamReader;

/ ** This example shows how to establish a connection * and send messages to the JMS queue. The classes in this * package operate on the same JMS queue. Run the classes together to * witness messages being sent and received, and to browse the queue * for messages. The class is used to send messages to the queue. ** @author Copyright (c) 1999-2003 by BEA Systems, Inc. All Rights Reserved. * / public class QueueSend {// Defines the JNDI context factory .public final static string jndi_factory = "WebLogic.jndi.wlinitialContextFactory";

// defines the jndi provider url.public final static string provider_url = "t3: // localhost: 80";

// defines the JMS Connection Factory for the Queue.public final static string jms_factory = "sendjmsFactory";

// defines the queue.public final static string queue = "sendjmsqueue";

Private QueueConnectionFactory QCONFACONNECTIONFAACTORY; Private Queuesession Qsession; Private Queuesender Qsender; Private Queue Queue; Private TextMessage MSG;

/ *** Creates all the necessary objects for sending * messages to a JMS queue. ** @param ctx JNDI initial context * @param queueName name of queue * @exception NamingException if operation can not be performed * @exception JMSException if JMS fails to initialize due to internal error * / 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 ();} / *** sends a message to a JMS queue ** @param message message to be sent * @exception JMSException if JMS fails to send message due to internal error * / public void send (String message) throws JMSException {msg.setText (message);. qsender.send (msg }

. / *** Closes JMS objects * @exception JMSException if JMS fails to close objects due to internal error * / public void close () throws JMSException {qsender.close (); qsession.close (); qcon.close (); .} / ** main () method ** @param args WebLogic Server URL * @exception Exception if operation fails * / public static void main (String [] args) throws Exception {InitialContext ic = getInitialContext (); QueueSend qs = new QUEUESEND (); QS.INIT (IC, Queue); Readandsend (QS); QS.Close ();}

private static void readAndSend (QueueSend qs) throws IOException, JMSException {BufferedReader msgStream = new BufferedReader (new InputStreamReader (System.in)); String line = null; boolean quitNow = false; do {System.out.print ( "Enter message ( / "quit /" to quit): "); line = msgstream.readline (); if (line! = null& line.trim (). Length ()! = 0) {qs.send (line); system. Out.println ("JMS Message Sent:" Line "/ N"); quitnow = line.equalsignorecase ("quit");}}}} while (! quitnow);

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

}

Six. Design Message Receive 1. JMS Resource Server URL: T3: // Localhost: 80 Connection Factory: SendjmsFactory Queue: Sendjmsqueue2. Design Steps · Initialization JNDI TreeHashTable Env = New HashTable (); Env.Put (Context.Initial_Context_Factory , JNDI_FACTORY); env.put (Context.PROVIDER_URL, PROVIDER_URL); return new InitialContext (env); · lookup ConnectionFactoryqconFactory = (QueueConnectionFactory) ctx.lookup (JMS_FACTORY); · lookup Destinationqueue = (Queue) ctx.lookup (queueName); • create a ConnectionFactory Connection qcon = qconFactory.createQueueConnection (); · create a Sessionqsession = qcon.createQueueSession (false, Session.AUTO_ACKNOWLEDGE) with the Connection; · create MessageConsumerqreceiver = qsession.createReceiver (queue) with Session and Destination; · set up listeners qreceiver .SetMessagelistener (this); · Start ConnectionQCon.Start (); 3. Source code package jmssample;

import java.util.Hashtable; import javax.jms *;. import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import java.util.Hashtable; import javax.jms *;. import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; / *** This example shows how to establish a connection to * and receive messages from a JMS queue The classes in this * package operate on. the same JMS queue. Run the classes together to * witness messages being sent and received, and to browse the queue * for messages. This class is used to receive and remove messages * from the queue. ** @author Copyright (c) 1999 . -2003 by BEA Systems, Inc. All Rights Reserved * / public class QueueReceive implements MessageListener {// Defines the JNDI context factory.public final static String JNDI_FACTORY = "weblogic.jndi.WLInitialContextFactory";

// defines the jndi provider url.public final static string provider_url = "t3: // localhost: 80";

// defines the JMS Connection Factory for the Queue.public final static string jms_factory = "sendjmsFactory";

// defines the queue.public final static string queue = "sendjmsqueue";

Private QueueConnectionFactory; private queuesession qsession; private queuereceiver qReceiver; private queue quit = false;

. / *** Message listener interface * @param msg message * / public void onMessage (Message msg) {try {String msgText; if (msg instanceof TextMessage) {msgText = ((TextMessage) msg) .getText ();} else {msgtext = msg.tostring ();

System.out.println ("Message Received: MsgText);

IF ("quit") {synchronized (this) {quit = true; this.notifyall (); // notify main thread to quit}}} catch (jmsexception jmse) {jmse.printStackTrace ();} } / *** Creates all the necessary objects for receiving * messages from a JMS queue. ** @param ctx JNDI initial context * @param queueName name of queue * @exception NamingException if operation can not be performed * @exception JMSException if JMS fails to initialize due to internal error * / 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); QReceiver = Qsession.createReceiver (queue); QReceiver.SetMequelistener (this); QCon.Start ();}

. / *** Closes JMS objects * @exception JMSException if JMS fails to close objects due to internal error * / public void close () throws JMSException {qreceiver.close (); qsession.close (); qcon.close (); } / *** main () method. ** @Param args weblogic server url * @Exception Exception if Execution Fails * /

Public static void main (string [] args) throws exception {

InitialContext IC = getInitialContext (); Queuereceive QR = new queuereceive (); qr.init (ic, queue);

System.out.println ("JMS Ready to Receive Messages (To Quit, Send A /" Quit / "Message).");

// Wait Until A "Quit" Message Has Been received.synchronized (qr) {while (! Qr.quit) {Try {qr.wait ();} catch (interruptedException IE)}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}} }

private static InitialContext getInitialContext () throws NamingException {Hashtable env = new Hashtable (); env.put (Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY); env.put (Context.PROVIDER_URL, PROVIDER_URL); return new InitialContext (env);}} VII. Test Message Send and Receive 1. Set WebLogic ClassPath;

2. Go to the Send Receiver Directory Compilation File;

3. Execute the acceptance process;

4. Open another window and execute the sender;

5. Enter the send message "quit", the receiver end. Summary This article briefly introduces some of the basic concepts of JMS, and then introduces the architecture and related features of WebLogic JMS Server. On this basis, the graphic is brightened, the configuration of JMS on WebLogic Server 8.1. Finally, while anatomy the JMS application framework, the JMS transmission and reception message flow is demonstrated with point-to-point.

转载请注明原文地址:https://www.9cbs.com/read-117424.html

New Post(0)