2012-07-03 87 views
2

我一直在網絡上幾天的文檔和帖子,但無法管理配置它的權利。與Spring 3.1集成HornetQ(2.2.x)

我正在開發獨立環境,我試圖在我的Spring3.1應用程序上簡單地集成HornetQ。

我知道我還必須修改Hornetq特定的xml的幾個。

它似乎總是缺少我的謎題。

任何完整的簡單工作示例?

非常感謝, 射線。

+0

下面是一個例子:http://www.javacodegeeks.com/2010/06/spring-3-hornetq-21-integration。 html?m = 1 – Nicholas

回答

2

您是使用HornetQ作爲嵌入式JMS服務器還是將它與JBoss一起使用?在這兩種情況下,它會有2個不同的答案尼古拉斯回答與嵌入式案例有關。因爲你說你在獨立環境下工作,所以我建議你不要在Spring中集成JMS,而是使用EE特性,比如EJB和MDB。它易於配置,並已在那裏。您所需要的只是正確配置它,爲消費者創建MDB併爲初始化彈簧配置創建@Singleton EJB。

這裏工作examplerussian original
編輯 所以,完整版:

  1. HornetQ的CONFIGS。我用inVm連接器在這裏放置了簡單的版本,但可以稍後將其更改爲netty。

hornetq-configuration.xml文件:

<configuration xmlns="urn:hornetq" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xsi:schemaLocation="urn:hornetq/schema/hornetq-configuration.xsd"> 

    <persistence-enabled>false</persistence-enabled> 
    <create-bindings-dir>true</create-bindings-dir> 
    <create-journal-dir>true</create-journal-dir> 
    <!--<create-paging-dir>true</create-paging-dir>--> 

    <journal-directory>/tmp/journal</journal-directory> 
    <paging-directory>/tmp/paging</paging-directory> 
    <bindings-directory>/tmp/binding</bindings-directory> 
    <!-- disable security --> 
    <security-enabled>false</security-enabled> 
    <!-- Connectors --> 

    <connectors> 
     <connector name="in-vm"> 
      <factory-class>org.hornetq.core.remoting.impl.invm.InVMConnectorFactory</factory-class> 
     </connector> 
    </connectors> 

    <acceptors> 
     <acceptor name="in-vm"> 
      <factory-class>org.hornetq.core.remoting.impl.invm.InVMAcceptorFactory</factory-class> 
     </acceptor> 
    </acceptors> 

    <!-- Other config --> 

    <!--<security-settings>--> 
     <!--&lt;!&ndash;security for example queue&ndash;&gt;--> 
     <!--<security-setting match="#">--> 
      <!--<permission type="createDurableQueue" roles="guest"/>--> 
      <!--<permission type="deleteDurableQueue" roles="guest"/>--> 
      <!--<permission type="createNonDurableQueue" roles="guest"/>--> 
      <!--<permission type="deleteNonDurableQueue" roles="guest"/>--> 
      <!--<permission type="consume" roles="guest"/>--> 
      <!--<permission type="send" roles="guest"/>--> 
     <!--</security-setting>--> 
    <!--</security-settings>--> 
</configuration> 

將hornetq-jms.xml文件:

<configuration xmlns="urn:hornetq" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xsi:schemaLocation="urn:hornetq /schema/hornetq-jms.xsd"> 
    <connection-factory name="ConnectionFactory"> 
     <connectors> 
      <connector-ref connector-name="in-vm"/> 
     </connectors> 
     <entries> 
      <entry name="ConnectionFactory"/> 
     </entries> 
     <consumer-window-size>0</consumer-window-size> 
     <retry-interval>1000</retry-interval> 
     <retry-interval-multiplier>1.5</retry-interval-multiplier> 
     <max-retry-interval>60000</max-retry-interval> 
     <reconnect-attempts>1000</reconnect-attempts> 
    </connection-factory> 

    <!--the queue used by the example--> 
    <queue name="statusQueue"> 
     <entry name="queue/statusQueue"/> 
    </queue> 
</configuration> 

如果啓用了安全性,還應該創建文件hornetq-users.xml中與用戶和角色說明。

彈簧配置:

<bean id="jmsServer" class="org.hornetq.jms.server.embedded.EmbeddedJMS" init-method="start" destroy-method="stop"/> 

    <bean id="jmsConnectionFactory" 
      class="mypackecge.JmsConnecitonFactoryLocator" 
      depends-on="jmsServer" 
      factory-method="lookupConnectionFactory"> 
     <constructor-arg name="server" ref="jmsServer"/> 
    </bean> 

    <bean id="jmsQueueTemplate" class="org.springframework.jms.core.JmsTemplate" depends-on="jmsServer"> 
     <property name="connectionFactory"> 
      <ref bean="jmsConnectionFactory"/> 
     </property> 
     <property name="destinationResolver"> 
      <ref bean="jmsDestResolver"/> 
     </property> 
    </bean> 

    <bean id="jmsDestResolver" class="mypackage.EmbeddedDestinationResolver"> 
     <property name="server" ref="jmsServer"/> 
    </bean> 

    <bean id="statusQueue" class="mypackage.JmsQueueLocator" 
      depends-on="jmsServer" 
      factory-method="lookupQueue"> 
     <constructor-arg name="server" ref="jmsServer"/> 
     <constructor-arg name="queueName" value="queue/statusQueue"/> 
    </bean> 

    <bean id="jmsContainerStatus" class="org.springframework.jms.listener.DefaultMessageListenerContainer"> 
     <property name="connectionFactory" ref="jmsConnectionFactory"/> 
     <property name="destination" ref="statusQueue"/> 
     <property name="messageListener" ref="statusChecker" /> 
     <property name="concurrentConsumers" value="5"/> 
    </bean> 

接着,查找JMS sessionFacroty和隊列創建的類。

目標解析器,由jmsTeplate用來解析發送消息的位置。

public class EmbeddedDestinationResolver implements DestinationResolver { 

    private EmbeddedJMS server; 

    public EmbeddedJMS getServer() { 
     return server; 
    } 

    public void setServer(EmbeddedJMS server) { 
     this.server = server; 
    } 

    @Override 
    public Destination resolveDestinationName(Session session, String destinationName, boolean pubSubDomain) throws JMSException { 
     return (Destination)server.lookup(destinationName); 
    } 
} 

連接工廠定位器。

public class JmsConnecitonFactoryLocator { 
    public static HornetQJMSConnectionFactory lookupConnectionFactory(EmbeddedJMS server){ 
     HornetQJMSConnectionFactory cf = (HornetQJMSConnectionFactory) server.lookup("ConnectionFactory"); 
     return cf; 
    } 
} 

隊列定位器。

public class JmsQueueLocator { 

    public static Queue lookupQueue(EmbeddedJMS server, String queueName){ 
     return (Queue) server.lookup(queueName); 
    } 
} 

消息處理程序。

@Component(value = "statusChecker") 
public class StatusChecker implements MessageListener { 

    private static final Logger logger = LoggerFactory.getLogger(StatusChecker.class); 

    @Override 
    public void onMessage(Message message) { 
     // handle message, call message.acknowledge() 
    } 

和用於在隊列發送消息JmsTemplate的樣品:

@Component 
public class WorkQueue { 

    private JmsTemplate jmsTemplate; 

    public JmsTemplate getJmsTemplate() { 
     return jmsTemplate; 
    } 

    @Autowired 
    public void setJmsTemplate(JmsTemplate jmsTemplate) { 
     this.jmsTemplate = jmsTemplate; 
    } 

    public void addStatusRequestToQueue(final Transaction t) throws JMSException { 

     jmsTemplate.send("queue/statusQueue", new MessageCreator() { 
      @Override 
      public Message createMessage(Session session) throws JMSException { 
       TextMessage msg = session.createTextMessage(); 
       msg.setText(String.valueOf(t.getId())); 
       return msg; 
      } 
     }); 
    } 
+0

嗨,其實我使用HornetQ與Spring集成而不是Jboss。 – rayman

+0

我更新了答案 – user1516873