JMS Application Deployment Tutorial for Beginners
Join the DZone community and get the full member experience.
Join For Freejms or java messaging service is an api for messaging (the exchange of information between two separate and independent network entities) based on java ee . it sets a number of interfaces for sending and receiving messages. there are two messaging models supported by jms: ptp or point-to-point and publish-subscribe. the first model (ptp) relies on the concept of message queues, where the key features are one consumer per message and no timing dependency between the sender and the receiver. the publish-subscribe model is vice versa: multiple consumers per message and a timing dependency between the sender and the receiver.
the main advantages of using jms are:
- asynchronous messaging (in this case, all the parts don’t need to be up for the app to function as a whole).
- storage (the messages are stored on behalf of the receiver while it is down and then sends them, once it is up.)
so, as usual, let’s try out this technology in the jelastic cloud using the glassfish server!
create environment
1. log into your jelastic account and click create environment .
2. select glassfish as your application server and specify the resource limits for it. then, type your environment name and click create .
in a few minutes your environment will appear on the jelastic dashboard.
create jms application
we're using a jms application, written by arun gupta , which consists of a sender, receiver and a simple test servlet for checking the results of program execution.
1. firstly, create sender. this is a stateless ejb that contains only one method for sending messages:
package org.sample.sendmessage; import javax.annotation.resource; import javax.ejb.stateless; import javax.jms.*; @stateless public class messagesender { @resource(mappedname = "jms/jmsconnectionfactory") private connectionfactory connectionfactory; @resource(mappedname = "jms/myqueue") queue queue; public void sendmessage(string message) { messageproducer messageproducer; textmessage textmessage; try { connection connection = connectionfactory.createconnection(); session session = connection.createsession(false, session.auto_acknowledge); messageproducer = session.createproducer(queue); textmessage = session.createtextmessage(); //textmessage.setjmsdeliverymode(deliverymode.non_persistent); textmessage.settext(message); messageproducer.send(textmessage); messageproducer.close(); session.close(); connection.close(); } catch (jmsexception e) { e.printstacktrace(); } } }
2. now we will create receiver, which is also ejb with a single method to receive messages synchronously:
package org.sample.sendmessage; import javax.annotation.resource; import javax.ejb.stateless; import javax.jms.*; import java.util.enumeration; @stateless public class messagereceiversync { @resource(mappedname = "jms/jmsconnectionfactory") private connectionfactory connectionfactory; @resource(mappedname = "jms/myqueue") queue myqueue; private string message; public string receivemessage() { try { connection connection = connectionfactory.createconnection(); session session = connection.createsession(false, session.auto_acknowledge); queuebrowser queuebrowser = session.createbrowser(myqueue); enumeration enumeration = queuebrowser.getenumeration(); while (enumeration.hasmoreelements()) { textmessage o = (textmessage) enumeration.nextelement(); return "receive " + o.gettext(); } session.close(); connection.close(); } catch (jmsexception e) { e.printstacktrace(); } return ""; } }
3. finally let’s create a test servlet which connects all the parts of our code and helps us to check the results of our program execution:
package org.sample.sendmessage; import java.io.ioexception; import java.io.printwriter; import javax.ejb.ejb; import javax.servlet.servletexception; import javax.servlet.annotation.webservlet; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; @webservlet(urlpatterns = {"/testservlet"}) public class testservlet extends httpservlet { @ejb messagesender sender; @ejb messagereceiversync receiver; protected void processrequest(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { response.setcontenttype("text/html;charset=utf-8"); try (printwriter out = response.getwriter()) { out.println(""); out.println(""); out.println("jms2 send message"); out.println(""); out.println(""); out.println("jms2 send/receive message using jms2 " + request.getcontextpath() + ""); string m = "hello there"; sender.sendmessage(m); out.format("message sent: %1$s.", m); out.println("receiving message..."); string message = receiver.receivemessage(); out.println("message rx: " + message); out.println(""); out.println(""); } } @override protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { processrequest(request, response); } @override protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { processrequest(request, response); } @override public string getservletinfo() { return "short description"; } }
4. build the application into a war package.
configure glassfish
1. open glassfish in a browser and log into the glassfish admin console using the credentials, which jelastic sent you when you created the environment.
2. java ee requires a jms connectionfactory to be configured and accessible to the application under the jndi name. firstly, navigate to jms resources and create a new factory called jms/jmsconnectionfactory with javax.jms.queueconnectionfactory type.
3. select gfcluster as a target and save the changes.
4. navigate to destination resources and create jms/myqueue queue called queue with javax.jms.queue type. the target is also gfcluster.
deploy jms application
1. navigate to the applications tab, upload your application package and deploy it to the send-message context of the glassfish cluster.
2. launch your jms application in a web browser to check the results.
as you can see any application, even complex ones work in the jelastic cloud like a charm. don’t just take our word for it. try jelastic and receive immediate access to your free 2 week free trial !
Published at DZone with permission of Marina Sprava, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
Manifold vs. Lombok: Enhancing Java With Property Support
-
A Comprehensive Guide To Testing and Debugging AWS Lambda Functions
-
How To Use Geo-Partitioning to Comply With Data Regulations and Deliver Low Latency Globally
-
Scaling Site Reliability Engineering (SRE) Teams the Right Way
Comments