AccountPublish.jws Sample

This topic inludes the source code for the AccountPublish.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 org.openuri.bea.samples.workshop.jms.account.AccountTransactionDocument;
006 import org.openuri.bea.samples.workshop.jms.account.AccountTransactionDocument.AccountTransaction;
007 import java.math.BigDecimal;
008 
009 
010 /**
011  <p> Demonstrates using a JMS control to publish to a JMS
012  * topic. The companion sample web service AccountSubscribe.jws
013  * listens to the same topic, so messages published by this web
014  * service will be received by AccountSubscribe.jws (if it
015  * is subscribed).</p>
016  
017  <p>Also demonstrates use of built-in logging facilities.
018  * The JwsContext.getLogger method will return a log4j
019  * logger that may be used by web services to write
020  * logging information to (by default) the workshop.log file.</p>
021  
022  <p>Log messages can have several levels:
023  * debug, info, warn, error and fatal. Note that a "debug"-level 
024  * log entry won't be accepted by the log file. By default, 
025  * logging for this server is configured to accept only 
026  * those messages of "info" level and higher. You can change 
027  * this configuration by editing the workshopLogCfg.xml file 
028  * installed with WebLogic Workshop. See the documentation for more
029  * information.</p>
030  
031  <p>Uses the AccountPublishJMSControl.jcx JMS control.</p>
032  * @common:target-namespace namespace="http://workshop.bea.com/AccountPublish"
033  */
034 public class AccountPublish implements com.bea.jws.WebService
035 
036     /**
037      * @common:context
038      */
039     JwsContext context;    
040     
041     /**
042      * @common:control
043      */
044     private jms.AccountPublishJMSControl accountPublishJMS;
045 
046     /**
047      <p>Accept transaction information for a deposit from
048      * the client and pass it to the JMS control to be
049      * published.</p>
050      *
051      * @common:operation
052      */
053     public void deposit(String accountID, double amount)
054     {
055         /*
056          * Get a logger with the name of this web service
057          * (that is a convention).
058          
059          * This web service is non-conversational, so it
060          * cannot persist the logger as a member variable.
061          */
062         Logger logger = context.getLogger("AccountPublish");
063         
064         /*
065          * Use the logger to write an "info"-level message
066          * to the log.
067          */
068         logger.info("about to publish (deposit)");
069         
070         /*
071          * Use the JMS control to publish a message to the
072          * topic for which it is configured (in
073          * AccountPublishJMSControl.jcx).
074          */
075         AccountTransactionDocument acctDoc = AccountTransactionDocument.Factory.newInstance();
076         AccountTransaction txn = acctDoc.addNewAccountTransaction();
077         txn.setTransactionAmount(new BigDecimal(amount));
078         accountPublishJMS.deposit(txn,accountID);
079         
080         logger.info("done publishing (deposit)");
081     }
082 
083     /**
084      <p>Accept transaction information for a withdrawal from
085      * the client and pass it to the JMS control to be
086      * published.</p>
087      *
088      * @common:operation
089      */
090     public void withdraw(String accountID, double amount)
091     {
092         /*
093          * Get a logger with the name of this web service
094          * (that is a convention).
095          
096          * This web service is non-conversational, so it
097          * cannot persist the logger as a member variable.
098          */
099         Logger logger = context.getLogger("AccountPublish");
100         
101         /*
102          * Use the logger to write an "info"-level message
103          * to the log.
104          */
105         logger.info("about to publish (withdraw)");
106 
107         /*
108          * Use the JMS control to publish a message to the
109          * topic for which it is configured (in
110          * AccountPublishJMSControl.jcx).
111          */
112         AccountTransactionDocument acctDoc = AccountTransactionDocument.Factory.newInstance();
113         AccountTransaction txn = acctDoc.addNewAccountTransaction();
114         txn.setTransactionAmount(new BigDecimal(amount));
115         accountPublishJMS.withdraw(txn, accountID);
116         
117         logger.info("done publishing (withdraw)");
118     }
119     
120     /**
121      * The context_onException callback handler can be defined in
122      * any web service that declared a JwsContext object (this one
123      * happens to declare a JwsContext object named "context").
124      
125      * Various errors that occur while executing this web service's
126      * methods will be reported by a call to this callback handler.
127      */
128     public void context_onException(Exception ex, String methodName, Object [] args)
129     {
130         /*
131          * Use the logger to write the exception's stack trace to
132          * the log file.  Exception.printStackTrace wants a
133          * java.io.PrintWriter, so make a java.io.StringWriter,
134          * wrap it as a PrintWriter and write the exception's
135          * stack trace to it.  Then write the StringWriter's
136          * string to the log.
137          */
138         Logger logger = context.getLogger("AccountPublish");
139         java.io.StringWriter writer = new java.io.StringWriter();
140         ex.printStackTrace(new java.io.PrintWriter(writer));
141         logger.info(writer.toString());
142     }
143   
144