Browse DevX
Sign up for e-mail newsletters from DevX


Build a Java App Server Foundation for Thick-Client Deployment : Page 2

Web 2.0, rich Internet applications, and heavy JavaScript have become the latest rage. But why build a Web application that tries to look and act like a thick client when you can leverage a traditional J2EE/Web application server architecture to easily deploy an actual thick client?




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Add Code to the StockTradeServer

For the purposes of this tutorial, your StockTradeServer will be very simple. You will create and expose two services: a StockService for requesting information about stocks and an OrderService to execute trades.

Your StockService will be as basic as you can get. Simply create a method to both get and set a list of stocks:
  1. Create interface StockService in package stephenlum.services.stock with two methods to get and set stocks:

    package stephenlum.services.stock; import java.util.List; import java.util.Map; import stephenlum.services.stock.dto.StockDTO; public interface StockService { public List<StockDTO> getStocks(List<String> tickerList); public void setStocks(Map<String, StockDTO> mapOfStocks); }

    Note: If you get an error stating parameterized types are available only in 5.0, then you need to change your compiler level to 5.0. You can do this by going to Window -> Preferences -> Compiler. If you don't have Java 5.0 installed, then you can just leave out the parameterized Type.

  2. Create the Data Transfer Object StockDTO in package stephenlum.services.stock.dto. Make sure that StockDTO implements Serializable, as it will be going over the wire. The following listing shows the numerous properties I've created for StockDTO:

    package stephenlum.services.stock.dto; import java.io.Serializable; /** * User: Stephen Lum */ public class StockDTO implements Serializable { private String tickerSymbol; private Double avgVol; private Double change; private String daysRange; private String fiftyTwoWeekRange; private Double lastTrade; private String marketCap; private Double volume; public StockDTO(String tickerSymbol, Double avgVol, Double change,
    String daysRange, String fiftyTwoWeekRange, Double lastTrade, String marketCap,
    Double volume) { this.tickerSymbol = tickerSymbol; this.avgVol = avgVol; this.change = change; this.daysRange = daysRange; this.fiftyTwoWeekRange = fiftyTwoWeekRange; this.lastTrade = lastTrade; this.marketCap = marketCap; this.volume = volume; } public Double getAvgVol() { return avgVol; } public Double getChange() { return change; } public String getDaysRange() { return daysRange; } public String getFiftyTwoWeekRange() { return fiftyTwoWeekRange; } public Double getLastTrade() { return lastTrade; } public String getMarketCap() { return marketCap; } public String getTickerSymbol() { return tickerSymbol; } public Double getVolume() { return volume; } }

  3. Create class StockServiceImpl in package stephenlum.services.stock. This class will implement interface StockService. After creating the class, implement the interface methods:

    package stephenlum.services.stock; import java.util.List; import java.util.Map; import java.util.ArrayList; import java.util.Iterator; import stephenlum.services.stock.dto.StockDTO; public class StockServiceImpl implements StockService { private Map mapOfStocks; public List<StockDTO> getStocks(List<String> tickerList) { List<StockDTO> resultList = new ArrayList<StockDTO>(); for (Iterator it = tickerList.listIterator(); it.hasNext();) { resultList.add((StockDTO)mapOfStocks.get(it.next())); } return resultList; } public void setStocks(Map<String, StockDTO> mapOfStocks) { this.mapOfStocks = mapOfStocks; } }

  4. Now declare your Spring Beans in applicationContext.xml. In this file, you declare your bean for StockService as well as instances of StockDTOs. For the StockDTOs, you create an instance for four companies: Microsoft, Sun, Oracle, and IBM:

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="stockService" class="stephenlum.services.stock.StockServiceImpl"> <property name="stocks"> <map> <entry key="msft" value-ref="msft"></entry> <entry key="sunw" value-ref="sunw"></entry> <entry key="orcl" value-ref="orcl"></entry> <entry key="ibm" value-ref="ibm"></entry> </map> </property> </bean> <bean id="msft" class="stephenlum.services.stock.dto.StockDTO"> <constructor-arg index="0"><!--ticker--> <value>MSFT</value> </constructor-arg> <constructor-arg index="1"><!--avgVol--> <value>75692500</value> </constructor-arg> <constructor-arg index="2"><!--change--> <value>-.16</value> </constructor-arg> <constructor-arg index="3"><!--daysRange--> <value>22.91 - 24.00</value> </constructor-arg> <constructor-arg index="4"><!--fiftyTwoWeekRange--> <value>23.03 - 28.38</value> </constructor-arg> <constructor-arg index="5"><!--lastTrade--> <value>22.99</value> </constructor-arg> <constructor-arg index="6"><!--marketCap--> <value>234.53B</value> </constructor-arg> <constructor-arg index="7"><!--volume--> <value>75692500</value> </constructor-arg> </bean> <bean id="sunw" class="stephenlum.services.stock.dto.StockDTO"> <constructor-arg index="0"><!--ticker--> <value>SUNW</value> </constructor-arg> <constructor-arg index="1"><!--avgVol--> <value>63942900</value> </constructor-arg> <constructor-arg index="2"><!--change--> <value>-.02</value> </constructor-arg> <constructor-arg index="3"><!--daysRange--> <value>4.56 - 4.70</value> </constructor-arg> <constructor-arg index="4"><!--fiftyTwoWeekRange--> <value>3.56 - 5.40</value> </constructor-arg> <constructor-arg index="5"><!--lastTrade--> <value>4.59</value> </constructor-arg> <constructor-arg index="6"><!--marketCap--> <value>16.05b</value> </constructor-arg> <constructor-arg index="7"><!--volume--> <value>66703285</value> </constructor-arg> </bean> <bean id="orcl" class="stephenlum.services.stock.dto.StockDTO"> <constructor-arg index="0"><!--ticker--> <value>ORCL</value> </constructor-arg> <constructor-arg index="1"><!--avgVol--> <value>47443800</value> </constructor-arg> <constructor-arg index="2"><!--change--> <value>-.07</value> </constructor-arg> <constructor-arg index="3"><!--daysRange--> <value>13.56 - 13.93</value> </constructor-arg> <constructor-arg index="4"><!--fiftyTwoWeekRange--> <value>11.75 - 15.21</value> </constructor-arg> <constructor-arg index="5"><!--lastTrade--> <value>13.70</value> </constructor-arg> <constructor-arg index="6"><!--marketCap--> <value>73.08B</value> </constructor-arg> <constructor-arg index="7"><!--volume--> <value>46390248</value> </constructor-arg> </bean> <bean id="ibm" class="stephenlum.services.stock.dto.StockDTO"> <constructor-arg index="0"><!--ticker--> <value>IBM</value> </constructor-arg> <constructor-arg index="1"><!--avgVol--> <value>5250390</value> </constructor-arg> <constructor-arg index="2"><!--change--> <value>-.38</value> </constructor-arg> <constructor-arg index="3"><!--daysRange--> <value>79.51 - 81.00</value> </constructor-arg> <constructor-arg index="4"><!--fiftyTwoWeekRange--> <value>73.45 - 89.94</value> </constructor-arg> <constructor-arg index="5"><!--lastTrade--> <value>80.28</value> </constructor-arg> <constructor-arg index="6"><!--marketCap--> <value>124.47B</value> </constructor-arg> <constructor-arg index="7"><!--volume--> <value>7019100</value> </constructor-arg> </bean> </beans>

Thanks for your registration, follow us on our social networks to keep up-to-date