为国争光的读后感600字:JBoss ESB with Eclipse入门例子
来源:百度文库 编辑:偶看新闻 时间:2024/04/29 13:59:49
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-->新增一个 location输入上面地址
有许多插件,只需选择JBoss AS Tools plugin和JBoss ESB Tools plugin
3: 在Eclipse里新建一个ESB项目:
安装好插件后点新建一个ESB项目 "New-->ESB-->ESB Project".
服务器可以选择JBOSS 或者ESB Server
Step 4:创建ESB project
首先在esbcontent/META-INF 下新建一个配置文件: jboss-esb.xml
输入如下内容
- version = "1.0" encoding = "UTF-8"?>
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">
-
-
name="JBossMQ" connection-factory="ConnectionFactory"> -
busid="quickstartGwChannel"> -
- dest-type="QUEUE"
- dest-name="queue/quickstart_helloworld_Request_gw" />
-
busid="quickstartEsbChannel"> -
- dest-type="QUEUE"
- dest-name="queue/quickstart_helloworld_Request_esb" />
-
-
- category="FirstServiceESB"
- name="SimpleListener"
- description="Hello World">
-
-
name="JMS-Gateway" - busidref="quickstartGwChannel"
- is-gateway="true"/>
-
name="helloWorld" - busidref="quickstartEsbChannel" />
-
mep="OneWay"> -
name="action1" - class="org.jboss.soa.esb.samples.quickstart.helloworld.MyJMSListenerAction"
- process="displayMessage"/>
process="displayMessage"/>
这个类根据自己实际路径修改
新建MyJMSListenerAction 类
Java代码
- 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;
- }
- }
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"?>
-
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">
-
optional-attribute-name="ServerPeer">jboss.messaging:service=ServerPeer -
jboss.messaging:service=PostOffice -
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">
-
optional-attribute-name="ServerPeer">jboss.messaging:service=ServerPeer -
jboss.messaging:service=PostOffice
jboss.messaging:service=ServerPeer jboss.messaging:service=PostOffice jboss.messaging:service=ServerPeer jboss.messaging:service=PostOffice
最后新建 esb 部署文件 deployment.xml 在 META-INF 文件夹下:
Xml代码jboss.esb.quickstart.destination:service=Queue,name=quickstart_helloworld_Request_esb -
jboss.esb.quickstart.destination:service=Queue,name=quickstart_helloworld_Request_gw
jboss.esb.quickstart.destination:service=Queue,name=quickstart_helloworld_Request_esb jboss.esb.quickstart.destination:service=Queue,name=quickstart_helloworld_Request_gw
发布工程到jboss服务器,并启动服务器
建立一个测试Client
Java代码- 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();
- }
- }
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();}}
运行客户端,会在服务器端看到
14:30:30,046 INFO [STDOUT] Body: Hello World