1 : 下載JBOSS ESB
http://www.jboss.org/jbossesb/downloads.html
本例使用版本4.7
2:安裝Eclipse JBOSS plugin
http://download.jboss.org/jbosstools/updates/nightly/trunk/
help-->install new soft-->新增一個(gè) location輸入上面地址
有許多插件,只需選擇JBoss AS Tools plugin和JBoss ESB Tools plugin
3: 在Eclipse里新建一個(gè)ESB項(xiàng)目:
安裝好插件后點(diǎn)新建一個(gè)ESB項(xiàng)目 "New-->ESB-->ESB Project".
服務(wù)器可以選擇JBOSS 或者ESB Server
Step 4:創(chuàng)建ESB project
首先在esbcontent/META-INF 下新建一個(gè)配置文件: jboss-esb.xml
輸入如下內(nèi)容
<?xml version = "1.0" encoding = "UTF-8"?><jbossesb xmlns="http://anonsvn.labs.jboss.com/labs/jbossesb/trunk/product/etc/schemas/xml/jbossesb-1.2.0.xsd"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://anonsvn.labs.jboss.com/labs/jbossesb/trunk/product/etc/schemas/xml/jbossesb-1.2.0.xsd http://anonsvn.jboss.org/repos/labs/labs/jbossesb/trunk/product/etc/schemas/xml/jbossesb-1.2.0.xsd"parameterReloadSecs="5"><providers><jms-provider name="JBossMQ" connection-factory="ConnectionFactory"><jms-bus busid="quickstartGwChannel"><jms-message-filterdest-type="QUEUE"dest-name="queue/quickstart_helloworld_Request_gw" /></jms-bus><jms-bus busid="quickstartEsbChannel"><jms-message-filterdest-type="QUEUE"dest-name="queue/quickstart_helloworld_Request_esb" /></jms-bus></jms-provider></providers><services><servicecategory="FirstServiceESB"name="SimpleListener"description="Hello World"><listeners><jms-listener name="JMS-Gateway"busidref="quickstartGwChannel"is-gateway="true"/><jms-listener name="helloWorld"busidref="quickstartEsbChannel" /></listeners><actions mep="OneWay"><action name="action1"class="org.jboss.soa.esb.samples.quickstart.helloworld.MyJMSListenerAction"process="displayMessage"/></actions></service></services></jbossesb>
<actions mep="OneWay">
<action name="action1"
class="org.jboss.soa.esb.samples.quickstart.helloworld.MyJMSListenerAction"
process="displayMessage"/>
這個(gè)類(lèi)根據(jù)自己實(shí)際路徑修改
新建MyJMSListenerAction 類(lèi)
package org.jboss.soa.esb.samples.quickstart.helloworld;import org.jboss.soa.esb.actions.AbstractActionLifecycle;import org.jboss.soa.esb.helpers.ConfigTree;import org.jboss.soa.esb.message.Message;public class MyJMSListenerAction extends AbstractActionLifecycle{protected ConfigTree _config;public MyJMSListenerAction(ConfigTree config) {_config = config;}public Message displayMessage(Message message) throws Exception{System.out.println("Body: " +message.getBody().get());return message;}}
在esbcontent目錄下新建: jbm-queue-service.xml .
<?xml version="1.0" encoding="UTF-8"?><server><mbean code="org.jboss.jms.server.destination.QueueService"name="jboss.esb.quickstart.destination:service=Queue,name=quickstart_helloworld_Request_esb"xmbean-dd="xmdesc/Queue-xmbean.xml"><depends optional-attribute-name="ServerPeer">jboss.messaging:service=ServerPeer</depends><depends>jboss.messaging:service=PostOffice</depends></mbean><mbean code="org.jboss.jms.server.destination.QueueService"name="jboss.esb.quickstart.destination:service=Queue,name=quickstart_helloworld_Request_gw"xmbean-dd="xmdesc/Queue-xmbean.xml"><depends optional-attribute-name="ServerPeer">jboss.messaging:service=ServerPeer</depends><depends>jboss.messaging:service=PostOffice</depends></mbean></server>
最后新建 esb 部署文件 deployment.xml 在 META-INF 文件夾下:
<jbossesb-deployment><depends>jboss.esb.quickstart.destination:service=Queue,name=quickstart_helloworld_Request_esb</depends><depends>jboss.esb.quickstart.destination:service=Queue,name=quickstart_helloworld_Request_gw</depends></jbossesb-deployment>
發(fā)布工程到j(luò)boss服務(wù)器,并啟動(dòng)服務(wù)器
package org.jboss.soa.esb.samples.quickstart.helloworld.test;import java.util.Properties;import javax.jms.JMSException;import javax.jms.ObjectMessage;import javax.jms.Queue;import javax.jms.QueueConnection;import javax.jms.QueueConnectionFactory;import javax.jms.QueueSender;import javax.jms.QueueSession;import javax.naming.Context;import javax.naming.InitialContext;import javax.naming.NamingException;public class SendJMSMessage {QueueConnection conn;QueueSession session;Queue que;public void setupConnection() throws JMSException, NamingException{Properties properties1 = new Properties();properties1.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");properties1.put(Context.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");properties1.put(Context.PROVIDER_URL, "jnp://127.0.0.1:1099");InitialContext iniCtx = new InitialContext(properties1);Object tmp = iniCtx.lookup("ConnectionFactory");QueueConnectionFactory qcf = (QueueConnectionFactory) tmp;conn = qcf.createQueueConnection();que = (Queue) iniCtx.lookup("queue/quickstart_helloworld_Request_gw");session = conn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);conn.start();System.out.println("Connection Started");}public void stop() throws JMSException{conn.stop();session.close();conn.close();}public void sendAMessage(String msg) throws JMSException {QueueSender send = session.createSender(que);ObjectMessage tm = session.createObjectMessage(msg);send.send(tm);send.close();}public static void main(String args[]) throws Exception{SendJMSMessage sm = new SendJMSMessage();sm.setupConnection();sm.sendAMessage("Hello World");//這里輸入消息文本sm.stop();}}
運(yùn)行客戶(hù)端,會(huì)在服務(wù)器端看到
14:30:30,046 INFO [STDOUT] Body: Hello World
聯(lián)系客服