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?
by Stephen Lum
Jun 6, 2006
Page 2 of 3
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.
StockService
Your StockService will be as basic as you can get. Simply create a method to both get and set a list of stocks:
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.
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:
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;
}
}
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: