本文我们来具体结合JBossESB中自带的helloworld_action例子,通过具体的HelloWorld中的配置来分析JBossESB的运行机制。
providers
和services
.<providers>
<jms-provider name="JBossMQ" connection-factory="ConnectionFactory">
<jms-bus busid="quickstartGwChannel">
<jms-message-filter
dest-type="QUEUE"
dest-name="queue/quickstart_helloworld_action_Request"
/>
</jms-bus>
<jms-bus busid="quickstartEsbChannel">
<jms-message-filter
dest-type="QUEUE"
dest-name="queue/quickstart_helloworld_action_esb"
/>
</jms-bus>
</jms-provider>
</providers>
首先是<providers>
,它是有一系列的<provider>
组成, 目前有jms-provider, fs-provider, ftp-provider等等. 然后我们在provider里面定义这个.esb文件里面service所定义的listener所需要的bus, Bus可以简单理解成消息传送所需要的传输层. 正如以下所显示的,我们定义了两个Bus,一个是给Gateway的Listener用,另外一个是给ESB-aware Message传输所需要的传输层.
1.2、 services的部分配置代码如下:
<services>
<service category="HelloWorld_ActionESB"
name="SimpleListener"
description="Hello World" >
<listeners>
<jms-listener name="JMS-Gateway"
busidref="quickstartGwChannel"
is-gateway="true"
/>
<jms-listener name="JMS-ESBListener"
busidref="quickstartEsbChannel"
/>
</listeners>
<actions mep="OneWay">
<action name="action2"
class="org.jboss.soa.esb.actions.SystemPrintln"/>
<action name="displayAction" class="org.jboss.soa.esb.samples.quickstart.helloworldaction.MyJMSListenerAction"
process="displayMessage">
<property name="exceptionMethod" value="exceptionHandler"/>
</action>
<action name="playAction"
class="org.jboss.soa.esb.samples.quickstart.helloworldaction.MyJMSListenerAction"
process="playWithMessage">
<property name="exceptionMethod" value="exceptionHandler"/>
</action>
<action name="notificationAction"
class="org.jboss.soa.esb.actions.Notifier">
<property name="okMethod" value="notifyOK" />
<property name="notification-details">
<NotificationList type="OK">
<target class="NotifyConsole" />
<target class="NotifyQueues">
<messageProp name="quickstart" value="hello_world_action" />
<queue jndiName="queue/quickstart_helloworld_action_Response"/>
</target>
</NotificationList>
</property>
</action>
<!-- The next action is for Continuous Integration testing -->
<action name="testStore" class="org.jboss.soa.esb.actions.TestMessageStore"/>
</actions>
</service>
</services>
第二部份就是定义services的部份, 在这里定义了当前这个esb包所提供的services. 每个service又是由 <listener>
和 <actions>
组成的.而actions又是由n个action组成。这里的action是对消息(Message)处理的地方.
在listener里,我们通过 busidref来关联到我们定义在provider里面的bus. 在这里,我们定义了两个listener. 其中一个是做为Gateway,只负责从外界获取到JMS的消息,然后转成ESB内部所需要的Message. 而另外一个listener是用来这个Message在services内部之间通讯的通道. 所以对于每个service来说,一定要至少定义一个listener来作为内部Message传输用.
public class MyJMSListenerAction extends AbstractActionLifecycle
{
protected ConfigTree _config;
public MyJMSListenerAction(ConfigTree config) { _config = config; }
public Message noOperation(Message message) { return message; }
public Message displayMessage(Message message) throws Exception{
logHeader();
System.out.println("Body: " + message.getBody().get().toString());
logFooter();
return message;
}
public Message playWithMessage(Message message) throws Exception {
// Header msgHeader = message.getHeader();
Body msgBody = message.getBody();
// Call theCall = msgHeader.getCall();
// EPR theEpr = theCall.getFrom();
String contents = msgBody.get().toString();
StringBuffer sb = new StringBuffer();
sb.append("/nBEFORE**/n");
sb.append(contents);
sb.append("/nAFTER**/n");
msgBody.add(sb.toString());
return message;
}
public void exceptionHandler(Message message, Throwable exception) {
logHeader();
System.out.println("!ERROR!");
System.out.println(exception.getMessage());
System.out.println("For Message: ");
System.out.println(message.getBody().get());
logFooter();
}
// This makes it easier to read on the console
private void logHeader() {
System.out.println("/n&&&&&&&&&&&&&&&&&&&&&&&&&&");
}
private void logFooter() {
System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&/n");
}
}
其中上述的方法会对应在jboss-esb.xml文件中的action。其中action中的name表示该action的一个别名,class表示定义该action方法所在的类(包及类名),process表示该类下的具体的方法。
/META-INF/jboss-esb.xml
/META-INF/deployment.xml 在这里定义对其他包或者服务的依赖,或者配置classloader.
jbm-queue-service.xml (optional) 这里是定义启动所需要的Queue
**.jar (optional) 放些所需要的第三方包
所需要的classes文件