JMS(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
- importjava.util.*;
- importjava.io.*;
- importjavax.jts.*;
- importjavax.jms.*;
- importjavax.naming.*;
- publicclass JMSQueueSend
- {
- publicstaticvoid main(String[] args)
- {
- try
- {
- // setup properties and server url
- Properties props =System.getProperties();
- props.put(Context.INITIAL_CONTEXT_FACTORY,
- "weblogic.jndi.WLInitialContextFactory");
- props.put(Context.PROVIDER_URL, "t3//localhost:7001");
- props.put(Context.SECURITY_PRINCIPAL, "weblogic");
- props.put(Context.SECURITY_CREDENTIALS, "password");
- // get the initial context
- InitialContext ctx =newInitialContext(props);
- // get the queue connection factory
- QueueConnectionFactory qconfactory =
- (QueueConnectionFactory)ctx.lookup(
- "javax.jms.QueueConnectionFactory");
- // create the connection
- QueueConnection qcon = qconfactory.createQueueConnection();
- // start the session
- QueueSession qsession = qcon.createQueueSession(false,
- Session.AUTO_ACKNOWLEDGE);
- // lookup queue
- Queue queue =(Queue)ctx.lookup("weblogic.examples.jms.exampleQueue");
- // create queue sender
- QueueSender qsender = qsession.createSender(queue);
- // create text message
- TextMessage msg = qsession.createTextMessage();
- qcon.start();
- // set the text
- msg.setText("JMS Message goes here");
- // send
- qsender.send(msg);
- }
- catch(Throwable t)
- {
- System.out.println("Exception caught: "+ t);
- }
- }
- }// end of class JMSQueueSend.