本文总结了在WebSphere Integration Developer 7下开发一个Hello World ESB Java Service的过程。
第1步 - 创建ESB Java service
1.1) 在WebSphere Integration Developer 7.0 workspace中, 切换到Business Integration Perspective.
1.2) 从文件菜单中, 选择File - New - Module, 输入HelloWorldServiceModule为Module名, 然后点击Finish.
1.3) 在HelloWorldServiceModule工程下, 右键点击Interfaces, 然后选择New - Integerface, 使用HelloWorldInterface为interface名称, 然后点击Finish.
1.4) 在interface编辑器中, 右键点击然后选择Add Request Response Operation, 使用hello为operation名称, 使用message为输入名称, response为输出名称, 两种同为string类型. 保存所有文件.
1.5) 切换到Assembly Editor, 从Palette中拖拉一个Java部件到Assembly Editor中, 命名为HelloWorldService.
1.6) 右键点击HelloWorldService, 然后选择Add - Interface, 选择HelloWorldInterface.
1.7) 右键HelloWorldService 然后选择Generate Implementation, 新建一个java package demo.
1.8) 切换到HelloWorldServiceImpl Java编辑器, 使用以下代码实现hello方法, 保存文件.
/**
* Method generated to support implementation of operation "hello" defined for WSDL port type
* named "HelloWorldInterface".
*
* Please refer to the WSDL Definition for more information
* on the type of input, output and fault(s).
*/
public String hello(String message) {
return "Hello:" + message;
}
1.9) 切换到Assembly Editor, 从Palette中拖拉一个References部件到Assembly Editor中.
1.10) 右键点击Stand-alone References, 然后选择Add Reference, 选择HelloWorldInterface
1.11) 连接Stand-alone References到HelloWorldService, 保存文件.
第2步 - 创建ESB客户端
2.1) 切换到Java EE perspective.
2.2) 右键点击HelloWorldServiceModuleWeb工程, 选择New - JSP, 命名为client.jsp, 点击Finish.
2.3) 在client.jsp中使用以下代码, 保存文件.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="com.ibm.websphere.sca.*,commonj.sdo.DataObject" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
<%
String serviceOutput = "N/A";
String message = request.getParameter("message");
if(message != null){
try{
ServiceManager serviceManager = new ServiceManager();
Service service = (Service) serviceManager.locateService("HelloWorldInterfacePartner");
DataObject respObject = (DataObject) service.invoke("hello", message);
serviceOutput = respObject.getString("response");
}catch(Exception e){
e.printStackTrace();
}
}
%>
Service Output : <%= serviceOutput %>
<form method="post">
<input type="text" NAME="message"/>
<input TYPE="submit" />
</form>
</body>
</html>
第3步 - 测试运行程序
3.1) 切换到Business Integration Perspective, 右键点击HelloWorldServiceModule, 选择Export和Integration modules and libraries - Files for server deployment
3.2) 将生成的EAR文件部署到Websphere Process Server上.
3.3) 使用浏览器访问client.jsp以测试该应用.