I have been going couple of docs and posts in the net for few days but couldn't manage configure it right.
我已经在网上写了好几篇文档和帖子,但是我没办法正确地配置它。
I am working on Standalone environment and I am trying to integrate simply HornetQ on my Spring3.1 application.
我正在开发一个独立的环境,我正在尝试将简单的HornetQ集成到Spring3.1应用程序中。
I know I have to modify also couple of Hornetq specific xml's.
我知道我还需要修改一些Hornetq特定的xml。
it's seems always something missing in my puzzle.
在我的困惑中似乎总是缺少一些东西。
Any full simple working sample?
任何完整的简单工作样本?
thanks alot, ray.
谢谢,雷。
1 个解决方案
#1
2
Are you using HornetQ as embedded JMS server or using it with JBoss? In both cases it will 2 different answers. Nicholas answer related to embedded case.
您是使用HornetQ作为嵌入式JMS服务器还是与JBoss一起使用?在这两种情况下,答案都是不同的。尼古拉斯的回答与嵌入式案例有关。
I suppose you are using HornetQ integrated in Jboss, because you say you working on Standalone environment, so I recommend you not integrate JMS in Spring, but using EE feature, like EJB and MDB. It easy configurable and already there. All you need is configure it properly, create MDB for consumers and @Singleton EJB for init spring configuration.
我认为您正在使用在Jboss中集成的HornetQ,因为您说您正在处理独立的环境,所以我建议您不要在Spring中集成JMS,而是使用EE特性,比如EJB和MDB。它易于配置,并且已经存在。您所需要的只是适当地配置它,为使用者创建MDB,并为init spring配置创建@Singleton EJB。
Here is working example (russian original)
EDIT So, full version:
这里是工作示例(俄文原文)编辑So,完整版本:
- HornetQ configs. I put here simple version with inVm connector, but you can change it to netty later.
- HornetQ配置。我在这里使用了inVm连接器的简单版本,但是稍后您可以将其更改为netty。
hornetq-configuration.xml:
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>-->
<!--<!–security for example queue–>-->
<!--<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:
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>
if you enable security, you should also creates hornetq-users.xml with user and roles descriptions.
如果启用安全性,也应该创建hornetq用户。带有用户和角色描述的xml。
spring config:
spring配置:
<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>
Next, creates classes for lookup jms sessionFacroty and queues.
接下来,为查找jms sessionFacroty和队列创建类。
Destnation resolver,used by jmsTeplate to resolve where to send message.
目的解析器,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);
}
}
Connection factory locator.
连接工厂定位器。
public class JmsConnecitonFactoryLocator {
public static HornetQJMSConnectionFactory lookupConnectionFactory(EmbeddedJMS server){
HornetQJMSConnectionFactory cf = (HornetQJMSConnectionFactory) server.lookup("ConnectionFactory");
return cf;
}
}
Queue locator.
队列定位器。
public class JmsQueueLocator {
public static Queue lookupQueue(EmbeddedJMS server, String queueName){
return (Queue) server.lookup(queueName);
}
}
Message handler.
消息处理程序。
@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()
}
and jmsTemplate sample for sending messages in queue:
和用于在队列中发送消息的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;
}
});
}
#1
2
Are you using HornetQ as embedded JMS server or using it with JBoss? In both cases it will 2 different answers. Nicholas answer related to embedded case.
您是使用HornetQ作为嵌入式JMS服务器还是与JBoss一起使用?在这两种情况下,答案都是不同的。尼古拉斯的回答与嵌入式案例有关。
I suppose you are using HornetQ integrated in Jboss, because you say you working on Standalone environment, so I recommend you not integrate JMS in Spring, but using EE feature, like EJB and MDB. It easy configurable and already there. All you need is configure it properly, create MDB for consumers and @Singleton EJB for init spring configuration.
我认为您正在使用在Jboss中集成的HornetQ,因为您说您正在处理独立的环境,所以我建议您不要在Spring中集成JMS,而是使用EE特性,比如EJB和MDB。它易于配置,并且已经存在。您所需要的只是适当地配置它,为使用者创建MDB,并为init spring配置创建@Singleton EJB。
Here is working example (russian original)
EDIT So, full version:
这里是工作示例(俄文原文)编辑So,完整版本:
- HornetQ configs. I put here simple version with inVm connector, but you can change it to netty later.
- HornetQ配置。我在这里使用了inVm连接器的简单版本,但是稍后您可以将其更改为netty。
hornetq-configuration.xml:
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>-->
<!--<!–security for example queue–>-->
<!--<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:
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>
if you enable security, you should also creates hornetq-users.xml with user and roles descriptions.
如果启用安全性,也应该创建hornetq用户。带有用户和角色描述的xml。
spring config:
spring配置:
<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>
Next, creates classes for lookup jms sessionFacroty and queues.
接下来,为查找jms sessionFacroty和队列创建类。
Destnation resolver,used by jmsTeplate to resolve where to send message.
目的解析器,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);
}
}
Connection factory locator.
连接工厂定位器。
public class JmsConnecitonFactoryLocator {
public static HornetQJMSConnectionFactory lookupConnectionFactory(EmbeddedJMS server){
HornetQJMSConnectionFactory cf = (HornetQJMSConnectionFactory) server.lookup("ConnectionFactory");
return cf;
}
}
Queue locator.
队列定位器。
public class JmsQueueLocator {
public static Queue lookupQueue(EmbeddedJMS server, String queueName){
return (Queue) server.lookup(queueName);
}
}
Message handler.
消息处理程序。
@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()
}
and jmsTemplate sample for sending messages in queue:
和用于在队列中发送消息的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;
}
});
}