AccountSubscribe.jws Sample

This topic inludes the source code for the AccountSubscribe.jws Sample.

Sample Location

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

BEA_HOME/weblogic81/samples/workshop/SamplesApp/WebServices/jms/

Sample Source Code


001 package jms; 
002 
003 import com.bea.control.JwsContext;
004 import weblogic.jws.util.Logger;
005 import java.text.SimpleDateFormat;
006 import java.util.Date;
007 import org.openuri.bea.samples.workshop.jms.account.AccountTransactionDocument;
008 import org.openuri.bea.samples.workshop.jms.account.AccountTransactionDocument.AccountTransaction;
009 import java.math.BigDecimal;
010 import java.util.HashMap;
011 import java.util.Map;
012 
013 
014 /**
015  <p> Demonstrates using a JMS control to subscribe to a JMS
016  * topic. The companion sample web service AccountPublish.jws
017  * publishes to the same topic, so messages published by that web
018  * service will be received by this one (if it is subscribed).</p>
019  
020  <p>Also demonstrates use of built-in logging facilities.
021  * The JwsContext.getLogger method will return a log4j
022  * logger that may be used by web services to write
023  * logging information to (by default) the workshop.log file.</p>
024  
025  <p>Log messages can have several levels:
026  * debug, info, warn, error and fatal. Note that a "debug"-level 
027  * log entry won't be accepted by the log file. By default, 
028  * logging for this server is configured to accept only 
029  * those messages of "info" level and higher. You can change 
030  * this configuration by editing the workshopLogCfg.xml file 
031  * installed with WebLogic Workshop. See the documentation for more
032  * information.</p>
033  
034  <p>Uses the AccountSubscribeJMSControl.jcx JMS control.</p>
035  * @common:target-namespace namespace="http://workshop.bea.com/AccountSubscribe"
036  */
037 public class AccountSubscribe implements com.bea.jws.WebService
038 
039     /**
040      * @common:context
041      */
042     JwsContext context;    
043     
044     /**
045      * @common:control
046      */
047     private jms.AccountSubscribeJMSControl accountSubscribeJMS;
048 
049     /*
050      * Declare a log4j logger as a member variable.  Make it
051      * static so it is not persisted.  Loggers have no state,
052      * so it wouldn't make any sense to persist it.
053      */
054     static private Logger logger;
055 
056     public Callback callback;
057 
058     public interface Callback
059     {
060         /**
061          <p>This callback relays the transaction data extracted from the
062          * arriving message to the client.</p>
063          
064          * @jws:conversation phase="continue"
065          */
066         public void accountUpdateReceived(
067                                             // JMS Header
068                                             String CorrelationID, 
069                                             String Type,
070                                             // JMS Properties
071                                             String accountID,
072                                             String transactionType, 
073                                             // Payload
074                                             BigDecimal amount);
075     }
076 
077     /**
078      <p>Start listening to the JMS topic. Messages that "arrive"
079      * while we are not listening will never be received.</p>
080      
081      * @common:operation
082      * @jws:conversation phase="start"
083      */
084     public void startListening()
085     {
086         /*
087          * Since we are starting a conversation, initialize the
088          * logger variable by getting a logger from the JwsContext
089          * object.
090          */
091         logger = context.getLogger("AccountSubscribe");
092         
093         /*
094          * use the logger to write a "info"-level message
095          * to the log.  Log messages can have several levels:
096          * debug, info, warn, error and fatal.
097          */
098         logger.info("about to subscribe");
099         
100         /*
101          * Subscribe to the topic to which
102          * AccountSubscribeJMSControl.jcx is configured to listen.
103          * No messages will be received by this service unless it
104          * is subscribed when the message is published.
105          */
106         accountSubscribeJMS.subscribe();
107         
108         logger.info("subscribed");
109     }
110 
111     /**
112      <p>Stop listening to the JMS topic.</p>
113      
114      * @common:operation
115      * @jws:conversation phase="finish"
116      */
117     public void stopListening()
118     {
119         logger.info("about to unsubscribe");
120         
121         /*
122          * Unsubscribe from the topic to which
123          * AccountSubscribeJMSControl.jcx is configured to listen.
124          * No messages will be received after this point unless
125          * we subscribe again.
126          */
127         accountSubscribeJMS.unsubscribe();
128         
129         logger.info("unsubscribed");
130     }
131 
132     /**
133      <p>Handler for the receiveUpdate callback of the JMS control.
134      * Just calls the client callback to relay the (slightly reordered)
135      * data extracted from the message.</p>
136      */
137     private void accountSubscribeJMS_receiveUpdate(
138                                                     //Message
139                                                     AccountTransaction transaction,
140                                                     // JMS Headers
141                                                     String CorrelationID, 
142                                                     String DeliveryMode,
143                                                     String Expiration, 
144                                                     String MessageID,
145                                                     String Priority, 
146                                                     String Redelivered,
147                                                     long Timestamp,                                                   
148                                                     String Type,
149                                                     // Properties                                                   
150                                                     String transactionType, 
151                                                     String accountID)                                                   
152     {
153         logger.info("received message, calling accountUpdateReceived callback");
154      
155         /*
156          * Call the client callback, passing the data received from the JMS control.
157          
158          * The Timestamp is converted from its native long format to a String format
159          * using a java.text.SimpleDateFormat object.
160          */
161         
162         callback.accountUpdateReceived(
163                                         CorrelationID,
164                                         Type,
165                                         accountID, 
166                                         transactionType, 
167                                         transaction.getTransactionAmount());
168                                                     
169         logger.info("called accountUpdateReceived callback");
170         if(accountID.equalsIgnoreCase("FINISH")) {
171             logger.info("calling fininsh");
172             accountSubscribeJMS.unsubscribe();
173             context.finishConversation();    
174         }
175     }
176     /**
177      * The context_onException callback handler can be defined in
178      * any web service that declared a JwsContext object (this one
179      * happens to declare a JwsContext object named "context").
180      
181      * Various errors that occur while executing this web service's
182      * methods will be reported by a call to this callback handler.
183      */
184     public void context_onException(Exception ex, String methodName, Object [] args)
185     {
186         /*
187          * Use the logger to write the exception's stack trace to
188          * the log file.  Exception.printStackTrace wants a
189          * java.io.PrintWriter, so make a java.io.StringWriter,
190          * wrap it as a PrintWriter and write the exception's
191          * stack trace to it.  Then write the StringWriter's
192          * string to the log.
193          */
194         java.io.StringWriter writer = new java.io.StringWriter();
195         ex.printStackTrace(new java.io.PrintWriter(writer));
196         logger.info(writer.toString());
197     }
198 
199