Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


Java Programming Code Examples

Java > Servlets Code Examples

Sending AJMS message from a servlet

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
Sending AJMS message from a servlet package com.ack.web.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.jms.*; import javax.naming.Context; import javax.naming.InitialContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * web.xml servlet configuration file * <web-app> ... <resource-ref> <description>outgoing jms send</description> <res-ref-name>jms/queue/connection</res-ref-name> <res-type>javax.jms.QueueConnectionFactory</res-type> <res-auth>CONTAINER</res-auth> </resource-ref> ... </web-app> * * the vendor-specific web.xml file that maps the res-ref-name * onto the one found in the jndi name space * <weblogic-web-app> ... <reference-descriptor> <resource-description> <res-ref-name>jms/queue/connection</res-ref-name> <jndi-name>jms/akira/connectionfactory</jndi-name> </resource-description> ... </reference-descriptor> ... </weblogic-web-app> */ public class SendingAJMSMessageFromAServlet extends HttpServlet { public void doGet( HttpServletRequest req, HttpServletResponse res ) throws ServletException, IOException { res.setContentType( "text/html" ); PrintWriter pw = res.getWriter(); QueueConnection queueCon = null; try { // get the queue connection factory Context ctx = new InitialContext(); QueueConnectionFactory qcf = (QueueConnectionFactory) ctx.lookup( "java:comp/env/jms/queue/connection" ); 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( "jms/queue/devilman" ); QueueSender sender = queueSession.createSender( queue ); Message msg = queueSession.createTextMessage( "hello..." ); sender.send( msg ); pw.println( "sent the message" ); } catch( Exception ex ) { log( "couldn't send the message", ex ); res.sendError( res.SC_INTERNAL_SERVER_ERROR, ex.getMessage() ); } finally { // close the queue connection if( queueCon != null ) { try { queueCon.close(); } catch( JMSException jme ) { log( "problem closing queue con" ); } } } } }