A simple example of JMS is that servlet is used as producer and message driven bean as consumer.

Source: Internet
Author: User
Tags glassfish

This example is used to familiarize yourself with the development process of JMS.

The result is that a servlet sends a message to a message driven Bean (MDB. The server is glassfish3.1.

 

First, create some JMS resources, including connectionfactory and a queue. In this example, it is a ptp jms link.

 

Establish a connection Factory

Start glassfish-> resources-> JMS resources-> link factory-> Create

The pool name (that is, the JNDI name) is JMS/queueconnectionfactory.

Select javax. JMS. queueconnectionfactory as the resource type, because we need a queue link.

Save

Create another target resource

JMS resource-destination resource-New

JNDI name: JMS/simplequeue

Physical Destination Address: simplequeue)

Resource Type: javax. JMS. Queue

After the resource is created, start to establish both ends of message communication, producer and consumer.

Create a servlet for producer

Eclipse creates a dynamic page project and a servlet named producer. The content is as follows:

Package COM. servlets; </P> <p> Import Java. io. ioexception; </P> <p> Import javax. annotation. resource; <br/> Import javax. JMS. connection; <br/> Import javax. JMS. jmsexception; <br/> Import javax. JMS. messageproducer; <br/> Import javax. JMS. queue; <br/> Import javax. JMS. queueconnectionfactory; <br/> Import javax. JMS. session; <br/> Import javax. JMS. textmessage; <br/> Import javax. naming. initialcontext; <br/> Import javax. naming. namingexception; <br/> Import javax. servlet. servletexception; <br/> Import javax. servlet. annotation. webservlet; <br/> Import javax. servlet. HTTP. httpservlet; <br/> Import javax. servlet. HTTP. httpservletrequest; <br/> Import javax. servlet. HTTP. httpservletresponse; </P> <p>/** <br/> * Servlet implementation class producer <br/> */<br/> @ webservlet ("/producer ") <br/> public class producer extends httpservlet {<br/> Private Static final long serialversionuid = 1l; </P> <p> // @ Resource (lookup = "JMS/simplequeue") <br/> Private Static queue; </P> <p> // @ Resource (lookup = "JMS/queueconnectionfactory") <br/> Private Static queueconnectionfactory connectionfactory; <br/>/** <br/> * @ see httpservlet # httpservlet () <br/> */<br/> Public producer () {<br/> super (); <br/> // todo auto-generated constructor stub <br/>}</P> <p>/** <br/> * @ see httpservlet # doget (httpservletrequest request, httpservletresponse response) <br/> */<br/> protected void doget (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {</P> <p> try <br/> {<br/> initialcontext jndicontext = new initialcontext (); <br/> connectionfactory = (queueconnectionfactory) jndicontext. lookup ("JMS/queueconnectionfactory"); // get the factory from app server <br/> queue = (Queue) jndicontext. lookup ("JMS/simplequeue"); // get the destination from app server <br/> connection = connectionfactory. createconnection (); <br/> session = connection. createsession (false, session. auto_acknowledge); <br/> messageproducer producer = session. createproducer (Queue); <br/> textmessage message = session. createtextmessage (); <br/> message. settext ("hello from a servlet *******************"); <br/> system. out. println ("producer in servlet has sent hello"); <br/> producer. send (Message); <br/> connection. close (); <br/>}< br/> catch (jmsexception E) <br/>{< br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>}< br/> catch (namingexception E) <br/>{< br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>}</P> <p>/** <br/> * @ see httpservlet # dopost (httpservletrequest request, httpservletresponse response) <br/> */<br/> protected void dopost (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {<br/> // todo auto-generated method stub <br/>}</P> <p >}< br/>

The original two resources can be injected through @ resource, but the problem is that @ resource has two different versions in JDK and glassfish, one (jee6) one (j2se) member does not exist in lookup. Therefore, an error occurs when injection is used. Therefore, you can use the old method to directly find resources from the JNDI context to initialize the member variables.

Servlet function is very simple, send a string to simplequeue.

Use glassfish to release the project export as war.

Create an MDB for consumer:

Create an EJB project in eclipse

Create a new class (An error occurred while creating MDB in eclipse)

 

 

 

JMS/simplequeue

JMS/simplequeue

JMS/queueconnectionfactory
JMS/simplequeue

The Code is as follows:

Package COM. JMS; </P> <p> Import javax. EJB. messagedriven; <br/> Import javax. JMS. jmsexception; <br/> Import javax. JMS. message; <br/> Import javax. JMS. messagelistener; <br/> Import javax. JMS. textmessage; </P> <p>/** <br/> * simle message driven bean. <br/> */<br/> @ messagedriven (mappedname = "JMS/simplequeue ") <br/> public class simplemessagedrivenbean implements messagelistener {</P> <p>/** <br/> * default constructor. <br/> */<br/> Public simplemessagedrivenbean () {<br/>}</P> <p>/** <br/> * @ see messagelistener # onmessage (Message) <br/> */<br/> Public void onmessage (message) <br/>{< br/> try <br/>{< br/> system. out. println ("got message:" <br/> + (textmessage) message ). gettext () + "@" <br/> + system. currenttimemillis (); <br/>}< br/> catch (jmsexception e) <br/>{< br/> E. printstacktrace (); <br/>}< br/>

The messagelistener method is implemented, and this class has the annotation of messagedriven, which is then mapped to simplequeue. In this way, the message is received in the onmessage method when the JMS message queue has a message.

Package the project as an EJB-jar and publish it with glassfish.

 

Before running the servlet, you only need to enter the corresponding servlet address in the browser address bar. Here I am using http: // localhost: 8080/jmsservlet/producer

The glassfish console will output the println of the message sent by the servlet and the println of the message received by the MDB.

JMS/simplequeue
JMS/simplequeue

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.