This procedure tells you how to modify the HelloJob to pass a parameter to it and make the job output a parameter.
The HelloJobBean is created.
...
1. In the Project Explorer, from the HelloJobProject open the HelloJobBean.java file.
2. Update the source code as shown in the sample below.
The sample below modifies the MDB class from the previous step. It demonstrates how you can pass a parameter as input for the job and how you create a parameter for the job’s output.
You use the getJobParameter() method from the JobContext interface to set a UserName parameter to the job that takes a string as input. You use the setJobParameter() method from the JobContext interface to set a NameLength parameter. The job processes the string input of the UserName parameter (calculates the number of characters in the string) and outputs the result of the calculation by logging a message in the database.
You specify the value of the UserName parameter’s input later when you create a scheduler task for the HelloJob. You do not specify the NameLengthparameter’s output anywhere, as it depends on the input of the UserName parameter.
You specify the data type and the direction of the parameters in the job-definition.xml at step Creating and Editing the job-definition.xml
package com.sap.scheduler.examples.hellojob;
import java.util.logging.Logger;
import com.sap.scheduler.runtime.JobContext; import com.sap.scheduler.runtime.JobParameter; import com.sap.scheduler.runtime.mdb.MDBJobImplementation; import javax.ejb.MessageDriven; import javax.ejb.ActivationConfigProperty;
@MessageDriven(activationConfig={ @ActivationConfigProperty( propertyName="messageSelector", propertyValue="JobDefinition='HelloJob'"), @ActivationConfigProperty( propertyName="destinationType", propertyValue="javax.jms.Queue")}) public class HelloJobBean extends MDBJobImplementation {
public void onJob(JobContext ctx) { String name; JobParameter nameParameter = ctx.getJobParameter("UserName"); if (nameParameter != null) { name = nameParameter.getStringValue(); } else { name = "Specify a name in the UserName field."; } Logger logger = ctx.getLogger(); logger.info("Hello " + name);
int nameLength = name.length();
logger.info("The length of your name is " + nameLength + " characters."); JobParameter lengthParameter = ctx.getJobParameter("NameLength"); lengthParameter.setIntegerValue(nameLength); ctx.setJobParameter(lengthParameter); }
} |
3. Save the file.
Editing the ejb-j2ee-engine.xml