Wednesday, January 30, 2008

BPEL - embedded Java

The example below shows a Java snippet defined in a BPEL Java activity





/* Start of Code snippet */

/*Write your java code below e.g.
System.out.println("Hello, World");
*/
try{

String status = "Bronze";

/* accessing a process variable of type string */
String country = (String)getVariableData("v_country");

/* accessing a variable within the complex type - customer */
Element country1 = (Element)getVariableData("inputVariable", "payload","/ns1:customer/ns1:Country");
String c1 = country1.getTextContent();

/* writes to the audit trail - will then be visible in BPEL console at runtime*/
addAuditTrailEntry("country is: " + country);
addAuditTrailEntry("country1 is: " + c1);

if (country.equalsIgnoreCase("Ireland")){
status = "Gold";
}

/* setting the output variable within the complex type - customer */
setVariableData("outputVariable", "payload",
"/ns1:customer/ns1:Status", status);

addAuditTrailEntry("status is: " + status);

}

/* End of Code snippet */
catch (Exception e) {
addAuditTrailEntry(e);
}

Add the following line before the bpelx:exec tag to import the Element class




Calling an external Java class in a Java activity

Jar up the Java class you want to use and copy the jarfile to the BPEL
system\services\lib directory.

In this example, my class is called CustomerStatus

/* Start of Code snippet */

try{

String status = "Bronze";
String country = (String)getVariableData("v_country");

Element country1 = (Element)getVariableData("inputVariable", "payload","/ns1:customer/ns1:Country");

String c1 = country1.getTextContent();

addAuditTrailEntry("country is: " + country);
addAuditTrailEntry("country1 is: " + c1);

CustomerStatus cs = new CustomerStatus();
status = cs.getStatus(country);

setVariableData("outputVariable", "payload",
"/ns1:customer/ns1:Status", status);

addAuditTrailEntry("status is: " + status);

}


catch (Exception e) {
addAuditTrailEntry(e);
}


/* End of Code snippet */


add the following entries before the bpelx:exec tag

1 comment:

George said...

thanks your share.
But can I write file in java embedding ,I try to write a log file in embedding java.But it did not work.There is not any file was created.There is the source:
/* Start of Code snippet */

/*Write your java code below e.g.
System.out.println("Hello, World");
*/
try{
Date Now = new Date();

/* accessing a process variable of type string */
String count = (String)getVariableData("Variable_countInt");

System.out.println("Loop:"+count+"\r\n");
System.out.println(Now.toLocaleString()+"\r\n\r\n");

File file = new File("/tmp/BpelTimerTest.log");
if (file.exists()){
if (file.isFile() && file.canWrite()){
FileWriter filewriter = new FileWriter(file, true);
filewriter.write("Loop:"+count+"\r\n");
filewriter.write(Now.toLocaleString()+"\r\n\r\n");
filewriter.close();
}else{
System.out.println("It can not be write");
}
}

}

/* End of Code snippet */
catch (Exception e) {
System.out.println(e);
}


Is it forbidden to write file in embedded java?