ClientXML.java Sample

This topic inludes the source code for the ClientXML.java Sample.

Sample Location

This sample is located in the following directory in your WebLogic Workshop installation:

BEA_HOME/weblogic81/samples/workshop/SamplesApp/WebServices/jms_xmlProtocol/

Sample Source Code


001 package jms_xmlProtocol;
002 
003 import java.io.IOException;
004 import java.util.Hashtable;
005 import javax.jms.*;
006 import javax.naming.Context;
007 import javax.naming.InitialContext;
008 import javax.naming.NamingException;
009 
010 /** 
011  * This example shows how to invoke a web service via a JMS queue.
012  */
013 public class ClientXML
014 {
015   // Defines the JNDI context factory.
016   public final static String JNDI_FACTORY="weblogic.jndi.WLInitialContextFactory";
017 
018   // Defines the JMS connection factory.
019   public final static String JMS_FACTORY="weblogic.jws.jms.QueueConnectionFactory";
020 
021   // Defines the queue.
022   public final static String QUEUE="jws.queue";
023 
024   private QueueConnectionFactory qconFactory;
025   private QueueConnection qcon;
026   private QueueSession qsession;
027   private QueueSender qsender;
028   private Queue queue;
029   private TextMessage msg;
030 
031   /**
032    * Creates all the necessary objects for sending
033    * messages to a JMS queue.
034    */
035   public void init(Context ctx, String queueName)
036     throws NamingException, JMSException
037   {
038     qconFactory = (QueueConnectionFactoryctx.lookup(JMS_FACTORY);
039     qcon = qconFactory.createQueueConnection();
040     qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
041     queue = (Queuectx.lookup(queueName);
042     qsender = qsession.createSender(queue);
043     msg = qsession.createTextMessage();
044     msg.setStringProperty("URI","/WebServices/jms_xmlProtocol/JMS_XMLProtocol.jws");
045    
046     qcon.start();
047   }
048 
049   /**
050    * Sends a message to a JMS queue.
051    */
052   public void send(String messagethrows JMSException {
053     msg.setText(message);
054     qsender.send(msg);
055   }
056 
057   /**
058    * Closes the JMS objects.
059    */
060   public void close() throws JMSException {
061     qsender.close();
062     qsession.close();
063     qcon.close();
064   }
065   
066   public void main(String[] argsthrows Exception 
067   {
068     InitialContext ic = getInitialContext(args[0]);
069     ClientXML qs = new ClientXML();
070     qs.init(ic, QUEUE);
071     readAndSend(qs, args[1]);
072     qs.close();
073   }
074 
075   private static void readAndSend(ClientXML qs, String name)
076     throws IOException, JMSException
077   {
078     String line = null;
079     boolean quitNow = false;
080     do 
081     {
082       line="<string xmlns=\"http://www.openuri.org/\">" + name + "</string>";
083       if (line != null && line.trim().length() != 0
084       {
085         qs.send(line);
086         System.out.println("JMS Message Sent: "+line+"\n");
087         quitNow = true;
088       }
089     while (! quitNow);
090 
091   }
092 
093   private static InitialContext getInitialContext(String url)
094     throws NamingException
095   {
096     Hashtable env = new Hashtable();
097     env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
098     env.put(Context.PROVIDER_URL, url);
099     return new InitialContext(env);
100   }
101 
102 }