为国争光的读后感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
输入如下内容

Xml代码
  1.  version = "1.0" encoding = "UTF-8"?>  
  2.  xmlns="http://anonsvn.labs.jboss.com/labs/jbossesb/trunk/product/etc/schemas/xml/jbossesb-1.2.0.xsd"  
  3.  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"  
  4.  parameterReloadSecs="5">  
  5.       
  6.            name="JBossMQ" connection-factory="ConnectionFactory">     
  7.                busid="quickstartGwChannel">     
  8.                        
  9.                       dest-type="QUEUE"     
  10.                       dest-name="queue/quickstart_helloworld_Request_gw" />     
  11.                    
  12.                busid="quickstartEsbChannel">     
  13.                        
  14.                       dest-type="QUEUE"     
  15.                       dest-name="queue/quickstart_helloworld_Request_esb"  />     
  16.                    
  17.      
  18.                
  19.            
  20.             
  21.            
  22.                
  23.             category="FirstServiceESB"       
  24.             name="SimpleListener"       
  25.             description="Hello World">     
  26.                  
  27.                  name="JMS-Gateway"     
  28.                     busidref="quickstartGwChannel"     
  29.                     is-gateway="true"/>     
  30.                  name="helloWorld"     
  31.                               busidref="quickstartEsbChannel" />     
  32.                  
  33.              mep="OneWay">     
  34.                     name="action1"       
  35.                     class="org.jboss.soa.esb.samples.quickstart.helloworld.MyJMSListenerAction"       
  36.                     process="displayMessage"/>     
  37.                       
  38.              
  39.            
  40.     
  41.    

  
                                       class="org.jboss.soa.esb.samples.quickstart.helloworld.MyJMSListenerAction"   
                    process="displayMessage"/> 
这个类根据自己实际路径修改

 

新建MyJMSListenerAction 类

 

Java代码
  1. package org.jboss.soa.esb.samples.quickstart.helloworld;   
  2.   
  3. import org.jboss.soa.esb.actions.AbstractActionLifecycle;   
  4. import org.jboss.soa.esb.helpers.ConfigTree;   
  5. import org.jboss.soa.esb.message.Message;   
  6.   
  7. public class MyJMSListenerAction extends AbstractActionLifecycle{   
  8.     protected ConfigTree  _config;      
  9.        
  10.       public MyJMSListenerAction(ConfigTree config) {       
  11.        _config = config;       
  12.       }       
  13.          
  14.       public Message displayMessage(Message message) throws Exception{      
  15.                   
  16.        System.out.println("Body: " +message.getBody().get());             
  17.        return message;       
  18.                           
  19.       }      
  20.   
  21. }  

 

 

在esbcontent目录下新建: jbm-queue-service.xml .

Xml代码
  1.  version="1.0" encoding="UTF-8"?>  
  2.   
  3.    code="org.jboss.jms.server.destination.QueueService"  
  4.     name="jboss.esb.quickstart.destination:service=Queue,name=quickstart_helloworld_Request_esb"  
  5.     xmbean-dd="xmdesc/Queue-xmbean.xml">  
  6.      optional-attribute-name="ServerPeer">jboss.messaging:service=ServerPeer  
  7.     jboss.messaging:service=PostOffice  
  8.     
  9.    code="org.jboss.jms.server.destination.QueueService"  
  10.     name="jboss.esb.quickstart.destination:service=Queue,name=quickstart_helloworld_Request_gw"  
  11.     xmbean-dd="xmdesc/Queue-xmbean.xml">  
  12.      optional-attribute-name="ServerPeer">jboss.messaging:service=ServerPeer  
  13.     jboss.messaging:service=PostOffice  
  14.     
  15.   

 

最后新建 esb 部署文件 deployment.xml 在 META-INF 文件夹下:

Xml代码
  1.      
  2. jboss.esb.quickstart.destination:service=Queue,name=quickstart_helloworld_Request_esb       
  3.   jboss.esb.quickstart.destination:service=Queue,name=quickstart_helloworld_Request_gw     
  4.   

 

 

发布工程到jboss服务器,并启动服务器

建立一个测试Client

Java代码
  1. package org.jboss.soa.esb.samples.quickstart.helloworld.test;   
  2.   
  3. import java.util.Properties;   
  4.   
  5. import javax.jms.JMSException;   
  6. import javax.jms.ObjectMessage;   
  7. import javax.jms.Queue;   
  8. import javax.jms.QueueConnection;   
  9. import javax.jms.QueueConnectionFactory;   
  10. import javax.jms.QueueSender;   
  11. import javax.jms.QueueSession;   
  12. import javax.naming.Context;   
  13. import javax.naming.InitialContext;   
  14. import javax.naming.NamingException;   
  15.   
  16. public class SendJMSMessage {   
  17.     QueueConnection conn;      
  18.     QueueSession session;      
  19.     Queue que;      
  20.           
  21.           
  22.     public void setupConnection() throws JMSException, NamingException      
  23.     {      
  24.         Properties properties1 = new Properties();      
  25.         properties1.put(Context.INITIAL_CONTEXT_FACTORY,      
  26.                 "org.jnp.interfaces.NamingContextFactory");      
  27.         properties1.put(Context.URL_PKG_PREFIXES,      
  28.                 "org.jboss.naming:org.jnp.interfaces");      
  29.         properties1.put(Context.PROVIDER_URL, "jnp://127.0.0.1:1099");      
  30.         InitialContext iniCtx = new InitialContext(properties1);      
  31.      
  32.         Object tmp = iniCtx.lookup("ConnectionFactory");      
  33.         QueueConnectionFactory qcf = (QueueConnectionFactory) tmp;      
  34.         conn = qcf.createQueueConnection();      
  35.         que = (Queue) iniCtx.lookup("queue/quickstart_helloworld_Request_gw");      
  36.         session = conn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);      
  37.         conn.start();      
  38.         System.out.println("Connection Started");      
  39.     }      
  40.           
  41.     public void stop() throws JMSException       
  42.     {       
  43.         conn.stop();      
  44.         session.close();      
  45.         conn.close();      
  46.     }      
  47.           
  48.     public void sendAMessage(String msg) throws JMSException {      
  49.               
  50.         QueueSender send = session.createSender(que);              
  51.         ObjectMessage tm = session.createObjectMessage(msg);      
  52.               
  53.         send.send(tm);              
  54.         send.close();      
  55.     }      
  56.              
  57.           
  58.     public static void main(String args[]) throws Exception      
  59.     {                         
  60.         SendJMSMessage sm = new SendJMSMessage();      
  61.         sm.setupConnection();      
  62.         sm.sendAMessage("Hello World");//这里输入消息文本       
  63.         sm.stop();       
  64.     }      
  65. }  

 

运行客户端,会在服务器端看到

14:30:30,046 INFO  [STDOUT] Body: Hello World