All custom payload types must extend from the IBMSensorEventPayload
class.
About this task
Follow these steps to create the custom payload.
Procedure
- In Rational® Application Developer for WebSphere® Software select and
click Next.
- Complete the following tasks in the Java Class window.
- For the source folder, enter TemperatureEvent_Java/src.
- For the package, enter com.temperature.event.payload.
- For the name, enter TemperatureEventPayload.
- For the modifiers, select Public.
- For the superclass, enter com.ibm.sensorevent.model.payload.IBMSensorEventPayload.
- Click Finish. The source
editor for TemperatureEventPayload.java file opens.
- 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.payload;
import com.ibm.sensorevent.model.IPayload;
import com.ibm.sensorevent.model.generic.IGenericGroup;
import com.ibm.sensorevent.model.generic.SensorEventException;
import com.ibm.sensorevent.model.payload.IBMSensorEventPayload;
public class TemperatureEventPayload extends IBMSensorEventPayload {
private static final long serialVersionUID = 1L;
// Constructor 1
protected TemperatureEventPayload() throws SensorEventException {
}
// Constructor 2
protected TemperatureEventPayload(String eventType) throws SensorEventException {
super(eventType);
}
// Factory method 1
public static IGenericGroup getInstance() throws SensorEventException {
return new TemperatureEventPayload();
}
// Factory method 2
public static IGenericGroup getInstance(String eventType) throws SensorEventException {
return new TemperatureEventPayload(eventType);
}
// Factory method 3
public static IGenericGroup getInstance(IPayload sourcePayload) throws SensorEventException {
TemperatureEventPayload payload = (TemperatureEventPayload) getInstance();
payload.copyFields(sourcePayload);
copyGroup(sourcePayload, payload);
return payload;
}
// Method 1
public int getTemperature() throws SensorEventException {
return this.eventGroup.getIntAttributeValue("temperature");
}
// Method 2
public void setTemperature(int t) throws SensorEventException {
this.eventGroup.addIntAttribute("temperature", t);
}
// Method 3
public long getTime() throws SensorEventException {
return this.eventGroup.getDateAttributeValueAsLong("time");
}
// Method 4
public void setTime(long t) throws SensorEventException {
this.eventGroup.addDateAttributeAsLong("time", t);
}
}
- Select .
- Select .
Example
In this custom payload example:
- Constructor 1 - Creates an empty payload. This constructor is protected
to prevent direct access.
- Constructor 2 - Creates a payload with the specified event type
- Factory method 1 - Creates an empty payload
- Factory method 2 - Creates a payload with the specified event type
- Factory method 3 - Creates a payload from the given payload
- Method 1 - Returns the temperature payload attribute
- Method 2 - Sets the temperature payload attribute
- Method 3 - Returns the time payload attribute
- Method 4 - Sets the time payload attribute
Every custom payload must contain the following:
- The same set of constructors
- The same set of factory methods
- Methods to manage the payload's attributes