Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The created task will read a text file at a specified location and places orders for stocks that are given in the text file is created. The text file has entries such as this one:

Code Block

IBM,100,120.50

Each line in the text file contains details for a stock order:

...

The complete code listing of the Task class is provided below:

Code Block

public class PlaceStockOrderTask implements Task, ManagedLifecycle {
private Log log = LogFactory.getLog(PlaceStockOrderTask.class);
private String to;
private String stockFile;
private SynapseEnvironment synapseEnvironment;

public void execute() {
log.debug("PlaceStockOrderTask begin");

if (synapseEnvironment == null) {
log.error("Synapse Environment not set");
return;    }

if (to == null) {
log.error("to not set");
return;    }

File existFile = new File(stockFile);

if(!existFile.exists()) {
log.debug("waiting for stock file");
return;    }

try {

// file format IBM,100,120.50

BufferedReader reader = new BufferedReader(new FileReader(stockFile));
String line = null;

while( (line = reader.readLine()) != null){
line = line.trim();

if(line == "") {
continue;
}

String[] split = line.split(",");
String symbol = split[0];
String quantity = split[1];
String price = split[2];
MessageContext mc = synapseEnvironment.createMessageContext();
mc.setTo(new EndpointReference(to));
mc.setSoapAction("urn:placeOrder");
mc.setProperty("OUT_ONLY", "true");
OMElement placeOrderRequest = createPlaceOrderRequest(symbol, quantity, price);
PayloadHelper.setXMLPayload(mc, placeOrderRequest);
synapseEnvironment.injectMessage(mc);
log.info("placed order symbol:" + symbol + " quantity:" + quantity + " price:" + price);
}

reader.close();
}

catch (IOException e) {
throw new SynapseException("error reading file", e);
}

File renamefile = new File(stockFile);
renamefile.renameTo(new File(stockFile + "." + System.currentTimeMillis()));
log.debug("PlaceStockOrderTask end");  }

public static OMElement createPlaceOrderRequest(String symbol, String qty, String purchPrice) {
OMFactory factory   = OMAbstractFactory.getOMFactory();
OMNamespace ns      = factory.createOMNamespace("http://services.samples/xsd", "m0");
OMElement placeOrder= factory.createOMElement("placeOrder", ns);
OMElement order     = factory.createOMElement("order", ns);
OMElement price     = factory.createOMElement("price", ns);
OMElement quantity  = factory.createOMElement("quantity", ns);
OMElement symb      = factory.createOMElement("symbol", ns);
price.setText(purchPrice);
quantity.setText(qty);
symb.setText(symbol);
order.addChild(price);
order.addChild(quantity);
order.addChild(symb);
placeOrder.addChild(order);
return placeOrder;
}

public void destroy() {
}

public void init(SynapseEnvironment synapseEnvironment) {
this.synapseEnvironment = synapseEnvironment;
}

public SynapseEnvironment getSynapseEnvironment() {
return synapseEnvironment;
}

public void setSynapseEnvironment(SynapseEnvironment synapseEnvironment) {
this.synapseEnvironment = synapseEnvironment;
}

public String getTo() {
return to;
}

public void setTo(String to) {
this.to = to;
}

public String getStockFile() {
return stockFile;
}

public void setStockFile(String stockFile) {
this.stockFile = stockFile;
}
}

...

Since a task implements ManagedLifecyle interface, ESB will call the init() method at the initialization of a Task object and destroy() method when a Task object is destroyed:

Code Block

public interface ManagedLifecycle {
public void init(SynapseEnvironment se);
public void destroy();
}

...

For example, the following properties in the Task class

Code Block

public String getStockFile() {
return stockFile;
}
public void setStockFile(String stockFile) {
this.stockFile = stockFile;
}

are initialized with the given values within the property element of the task in the configuration.

Code Block

<syn:property xmlns="http://ws.apache.org/ns/synapse" name="stockFile"value="/home/upul/test/stock.txt"/>

For those properties given as XML elements, properties need to be defined within the Task class using the following format:

Code Block

public void setMessage(OMElement elem) {
message = elem;}

...

(OMElement comes from Apache AXIOM which is used by WSO2 ESB. AXIOM is an object model similar to DOM. To learn more about AXIOM, see the AXIOM tutorial.) 

Code Block

<property name="message">
<m0:getQuote xmlns:m0="http://services.samples/xsd">
<m0:request>
<m0:symbol>IBM</m0:symbol>
</m0:request>
</m0:getQuote>
</property>

...

1. Log in to the ESB console.

Image Removed

2. Click the "Main" button to access the "Manage" menu.

...

10. Copy the stockfile.txt to a new directory. The stockfile.txt will contain the following entries:

Code Block

IBM,100,120.50
MSFT,200,70.25
SUN,400,60.758.

...

12. Enter 15000 to "Interval."

13. Click "Schedule."

Excerpt
hiddentrue

Instructions on how to create a custom task for WSO2 ESB.