Quantcast
Channel: Developer Feed - Snippet
Viewing all articles
Browse latest Browse all 178

How to send a JMS Message?

$
0
0

JMS Sender ProgramJMS(Java Message Service) provides a reliable way to sending message asynchronously. The message are sent to a queue(destination) and consumer like MDB is hooked up the queue. The following snippet shows how to send a JMS message as Text in Weblogic Server.<!--break-->

Snippet

  1. importjava.util.*;
  2. importjava.io.*;
  3. importjavax.jts.*;
  4. importjavax.jms.*;
  5. importjavax.naming.*;
  6.  
  7. publicclass JMSQueueSend
  8. {
  9.   publicstaticvoid main(String[] args)
  10.   {
  11.     try
  12.     {
  13.       // setup properties and server url
  14.       Properties props =System.getProperties();
  15.       props.put(Context.INITIAL_CONTEXT_FACTORY,
  16.         "weblogic.jndi.WLInitialContextFactory");
  17.       props.put(Context.PROVIDER_URL, "t3//localhost:7001");
  18.       props.put(Context.SECURITY_PRINCIPAL, "weblogic");
  19.       props.put(Context.SECURITY_CREDENTIALS, "password");
  20.  
  21.       // get the initial context
  22.       InitialContext ctx =newInitialContext(props);
  23.  
  24.       // get the queue connection factory
  25.       QueueConnectionFactory qconfactory =
  26.         (QueueConnectionFactory)ctx.lookup(
  27.             "javax.jms.QueueConnectionFactory");
  28.  
  29.       // create the connection
  30.       QueueConnection qcon = qconfactory.createQueueConnection();
  31.  
  32.       // start the session
  33.       QueueSession qsession = qcon.createQueueSession(false,
  34.             Session.AUTO_ACKNOWLEDGE);
  35.  
  36.      // lookup queue
  37.       Queue queue =(Queue)ctx.lookup("weblogic.examples.jms.exampleQueue");
  38.  
  39.       // create queue sender
  40.       QueueSender qsender = qsession.createSender(queue);
  41.  
  42.       // create text message
  43.       TextMessage msg = qsession.createTextMessage();
  44.       qcon.start();
  45.  
  46.       // set the text
  47.       msg.setText("JMS Message goes here");
  48.  
  49.      // send
  50.       qsender.send(msg);
  51.     }
  52.     catch(Throwable t)
  53.     {
  54.       System.out.println("Exception caught: "+ t);
  55.     }
  56.   }
  57.  
  58. }// end of class JMSQueueSend.

Viewing all articles
Browse latest Browse all 178

Trending Articles