All task agent MDBs must extend from IBMSEAbstractTaskAgent class.
About this task
Follow these steps to create a task agent MDB.
Procedure
- In Rational® Application Developer for WebSphere® Software, select TemperatureEvent_EJB.
- Select and click Next.
- Complete the following tasks in the Enterprise Bean window.
- Select Message-drive bean.
- For the EJB project, select TemperatureEvent_EJB.
- For the bean name, enter TemperatureEventTaskAgent.
- For the source folder, enter ejbModule.
- For the default package, enter com.temperature.event.mdb.
- Click Next.
- In the Message-Driven Bean Type window, select JMS type and click Next.
- In the Create a JMS Message-Driven Bean window, accept all the defaults and click Next.
- In the EJB Java Class Details window, enter com.ibm.sensorevent.engine.baseagent.IBMSEAbstractTaskAgent for the bean superclass.
- Click Next.
- If the Confirm Enablement window appears, select Always enable activities and don't ask me againand click OK.
- Close the class diagram editor (default.dnx) when it opens. It will not be used.
- In the Project Explorer view, expand .
- Double-click TemperatureEventTaskAgentBean.java to open the source editor There are errors in the generated code which will be corrected in the next step.
- Change the source to be the following:
/**********************************************************************************
* Licensed Materials - Property of IBM
* 5724-L17 WebSphere Premises Server
* (c) Copyright IBM Corp. 2008 All rights reserved.
*
* US Government Users Restricted Rights - Use, duplication or disclosure
* restricted by GSA ADP Schedule Contract with IBM Corp.
*
* DISCLAIMER OF WARRANTIES. The following code is sample code created by
* IBM Corporation. This sample code is part of the WebSphere Premises Server
* and is warranted to perform its intended function only if used un-modified.
* If you modify this code then it is considered provided "AS IS", without
* warranty of any kind. Notwithstanding the foregoing, IBM shall not be liable
* for any damages arising out of your use of the sample code, even if they have
* been advised of the possibility of such damages.
***********************************************************************************/
package com.temperature.event.mdb;
import com.ibm.sensorevent.model.IPayloadMetaData;
import com.ibm.sensorevent.model.ISensorEvent;
import com.temperature.event.payload.TemperatureEventPayload;
public class TemperatureEventTaskAgentBean
extends com.ibm.sensorevent.engine.baseagent.IBMSEAbstractTaskAgent
implements javax.ejb.MessageDrivenBean, javax.jms.MessageListener {
private static final long serialVersionUID = 1L;
private javax.ejb.MessageDrivenContext fMessageDrivenCtx;
// Method 1
protected void onIBMSensorEvent(ISensorEvent event) {
try {
// display the event data
TemperatureEventPayload payload = (TemperatureEventPayload) event.getPayload();
int temperature = payload.getTemperature();
long time = payload.getTime();
System.out.println("task agent: temperature = " + temperature);
System.out.println("task agent: time = " + time);
// add sample data to event metadata
IPayloadMetaData metadata = event.getPayloadMetaData();
metadata.addBooleanAttribute("processed", true);
// forward event to output channels
this.publishOutbound(event);
} catch (Exception e) {
e.printStackTrace();
}
}
// Method 2
public void onMessage(javax.jms.Message msg) {
super.onMessage(msg);
}
public javax.ejb.MessageDrivenContext getMessageDrivenContext() {
return fMessageDrivenCtx;
}
public void setMessageDrivenContext(javax.ejb.MessageDrivenContext ctx) {
fMessageDrivenCtx = ctx;
}
public void ejbCreate() {
}
public void ejbRemove() {
}
}
- Select .
- Select .
Example
In this task agent example:
- Method 1 - Overrides method onIBMSensorEvent as defined in IBmSEAbstractTaskAgent. This method handles events delivered to this task agent MDB. In this example
- The payload is retrieved from the event
- The payload attributes are displayed to the console
- Sample data is added to the event's payload metadata
- The event is sent to any registered output channel
- Method 2 - Passes processing of the JMS message to the superclass so that a call is automatically made to onIBMSensorEvent by IBMSEAbstractTaskAgent
- The remaining methods are generated by the EJB wizard
Every task agent MDB must contain the following:
- The onIBMSensorEvent method to handle the event
- The onMessage method to pass processing to the superclass