📄 stockswsmanager.java
字号:
/** * StockWSWSManager acts as a subscriber of a webservice * It reuses a webservice, which provides stock quotes * Messages are received as java object and gets * converted into predefined data access object, which can be * used in the jsp pages to display the stock quote values * It uses JAX-RPC,WSDL and SOAP to access the webservice. * * * */ /** * Change History: * Author Date Version Details * Jerome Josephraj 28 October 2002 1.00.01 Created */ package com.ddj.wsstruts.wsmanager.subscriber;import javax.xml.rpc.namespace.QName;import java.util.Vector;//Log4J statementsimport org.apache.log4j.Category;public class StocksWSManager { /** Creates new StocksWS */ public StocksWSManager() { } //Log4J // define a static category variable so that it references the // category instance of the same name as this class. static final Category category = Category.getInstance(StocksWSManager.class.getName()); static { // Setup the global JAXM message factory System.setProperty("javax.xml.soap.MessageFactory", "weblogic.webservice.core.soap.MessageFactoryImpl"); // Setup the global JAX-RPC service factory System.setProperty( "javax.xml.rpc.ServiceFactory", "weblogic.webservice.core.rpc.ServiceFactoryImpl"); } /* * This method uses an external publisher * to publish the stock quotes required * It uses JAX-RPC apis to invoke the webservice * and gets the result as Float objects and converts it to * a pre-defined data access object. * This method is called by Struts concrete action class (NewsAction). The * result is passed back to the NewsAction class as Vector object, which * all the necessary predefined data access object. * Any exceptions are thrown as webservice exception, this is done by * setting a value in java exception (A seperate webservice exception object * can be created to have more flexibility and to handle more complex error * messages). * * @return Vector object */ public Vector getStocks() throws Exception { //Method level variables Vector stockValues = new Vector(); try { // create service factory javax.xml.rpc.ServiceFactory factory = javax.xml.rpc.ServiceFactory.newInstance(); // define targetNameSpace String targetNamespace = "http://www.themindelectric.com/" + "wsdl/net.xmethods.services.stockquote.StockQuote/"; // define qname QName serviceName = new QName(targetNamespace, "net.xmethods.services.stockquote.StockQuoteService"); // define porname QName portName = new QName(targetNamespace, "net.xmethods.services.stockquote.StockQuotePort"); // define operation name QName operationName = new QName("urn:xmethods-delayed-quotes", "getQuote"); //Specify wsdl location java.net.URL wsdlLocation = new java.net.URL("http://services.xmethods.net/soap/urn:xmethods-delayed-quotes.wsdl"); // create service javax.xml.rpc.Service service = factory.createService(wsdlLocation, serviceName); // create call javax.xml.rpc.Call call = service.createCall(portName, operationName); //Populate an array with list of stock names Vector populatedStockNames = this.getPopulatedList(); //Loop through the populated list, and for each stock get a result java.util.Iterator i = this.getPopulatedList().iterator(); String stockName = null; Float result; com.ddj.wsstruts.valueobject.StockValue stockValue = null; while(i.hasNext()) { stockName = (String) i.next(); // invoke the remote web service result = (Float) call.invoke(new Object[] {stockName}); if(category.isDebugEnabled()) { category.debug(" The quote for " + stockName + " is: " + result); } //Set stock name and stock values in stockValue bean stockValue = new com.ddj.wsstruts.valueobject.StockValue(); stockValue.setStockName(stockName); stockValue.setStockValue(result.toString()); //Add the stockValue bean to stockValues vector stockValues.add(stockValue); } } catch(javax.xml.rpc.JAXRPCException rpce) { if(category.isDebugEnabled()) { category.error(rpce); } //throw it as an exception throw new Exception(com.ddj.wsstruts.constant.SystemConstants.WEBSERVICE_EXCEPTION); } catch(javax.xml.rpc.ServiceException se) { se.printStackTrace(); //throw it as an exception throw new Exception(com.ddj.wsstruts.constant.SystemConstants.WEBSERVICE_EXCEPTION); } catch(Exception e) { //Print the exception in the console e.printStackTrace(); throw e; } return stockValues; } /* *This method populates stock names in a vector. * This is done for example purpose only. * In a real world situation, these values will get populated from * a datasource * * @return Vector object */ public Vector getPopulatedList() { Vector populatedStockNames = new Vector(); populatedStockNames.add("LMIN"); populatedStockNames.add("MSFT"); populatedStockNames.add("CSCO"); populatedStockNames.add("SUNW"); populatedStockNames.add("INTC"); populatedStockNames.add("HOMS"); return populatedStockNames; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -