Breaking News
Home / ServerSide / Weblogic / How to send a JMS Message?

How to send a JMS Message?

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.<!>

Snippet

  1. import java.util.*;
  2. import java.io.*;
  3. import javax.jts.*;
  4. import javax.jms.*;
  5. import javax.naming.*;
  6.  
  7. public class JMSQueueSend
  8. {
  9.   public static void 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 = new InitialContext(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.
[pb_builder]

Check Also

Error – Remote side declared peer gone on this JVM

Issue When trying to sent a JMS message to a remote Weblogic Application Server the …

Advertisment ad adsense adlogger