如何在春季启动应用程序中使用HornetMQ消息

时间:2018-12-02 18:39:35

标签: java spring spring-boot

我正在尝试将HornetMQ Consumer集成到Springboot应用程序中。我看到了不同的示例,但是所有示例都指向ActiveMQ实现,这使我有些困惑。我已经用Java编写了一个标准的HornetQ Consumer。这是代码:

public class HornetQClient {

private String JMS_QUEUE_NAME;
private String MESSAGE_PROPERTY_NAME;

private ClientSessionFactory sf = null;
private static final Logger LOGGER = LoggerFactory.getLogger(TCPClient.class);

public HornetQClient(String hostName, String hostPort, String queueName, String propertyName) {
    try {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("host", hostName);
        map.put("port", hostPort);
        this.JMS_QUEUE_NAME = queueName;
        this.MESSAGE_PROPERTY_NAME = propertyName;

        ServerLocator serverLocator = HornetQClient.createServerLocatorWithoutHA(new TransportConfiguration(NettyConnectorFactory.class.getName(), map));
        sf = serverLocator.createSessionFactory();
        startReadMessages();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

private void startReadMessages() {
    ClientSession session = null;
    try {
        if (sf != null) {
            session = sf.createSession(true, true);

            while (true) {
                ClientConsumer messageConsumer = session.createConsumer(JMS_QUEUE_NAME);
                session.start();

                ClientMessage messageReceived = messageConsumer.receive(1000);
                if (messageReceived != null && messageReceived.getStringProperty(MESSAGE_PROPERTY_NAME) != null) {
                    System.out.println("Received JMS TextMessage:" + messageReceived.getStringProperty(MESSAGE_PROPERTY_NAME));
                    messageReceived.acknowledge();
                } else
                    System.out.println("no message available");
                messageConsumer.close();
                Thread.sleep(500);
            }
        }
    } catch (Exception e) {
        LOGGER.error("Error while adding message by producer.", e);
    } finally {
        try {
            session.close();
        } catch (HornetQException e) {
            LOGGER.error("Error while closing producer session,", e);
        }
    }
}

这很好用,但是有什么标准方法可以在Spring Boot应用程序中编写Message Consumer,或者我应该直接创建此客户端的Bean并在Springboot应用程序中使用

--------------- hornetq-jms.xml ---------

 <?xml version="1.0"?>
<configuration xsi:schemaLocation="urn:hornetq /schema/hornetq-jms.xsd"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:hornetq">
    <!--the connection factory used by the example -->
    <connection-factory name="ConnectionFactory">
        <connectors>
            <connector-ref connector-name="netty-connector" />
        </connectors>
        <entries>
            <entry name="ConnectionFactory" />
        </entries>
        <consumer-window-size>0</consumer-window-size>
        <connection-ttl>-1</connection-ttl>
    </connection-factory>
    <queue name="trackerRec">
        <entry name="trackerRec" />
    </queue>
</configuration>

1 个答案:

答案 0 :(得分:0)

您可能为此使用Spring JMS和JmsTemplate。 Spring引导的默认设置是使用ActiveMQ连接工厂,但是如果将其交换为HornetQConnetionFactory,则应该很好:

@Configuration
@EnableJms
public class JmsConfig {
    @Bean
    public ConnectionFactory connectionFactory() {
        final Map<String, Object> properties = new HashMap<>();
        properties.put("host", "127.0.0.1");
        properties.put("port", "5445");
        final org.hornetq.api.core.TransportConfiguration configuration =
            new org.hornetq.api.core.TransportConfiguration("org.hornetq.core.remoting.impl.netty.NettyConnectorFactory", properties);
        return new org.hornetq.jms.client.HornetQJMSConnectionFactory(false, configuration);
    }

    @Bean
    public JmsListenerContainerFactory<?> myFactory(ConnectionFactory connectionFactory,
                                                    DefaultJmsListenerContainerFactoryConfigurer configurer) {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        configurer.configure(factory, connectionFactory);
        return factory;
    }
}

为了清楚起见,我添加了全限定的类名。

然后,在某些bean中,您可以执行以下操作以消耗一条消息:

@JmsListener(destination = "some.queue", containerFactory = "myFactory")
public void receiveMessage(@Header("some.header") final String something) {
    System.out.println("Received <" + something + ">");
}

免责声明:我实际上并没有实际尝试过,这是基于我对Spring和ActiveMQ的经验以及以下来源:https://dzone.com/articles/connecting-spring https://spring.io/guides/gs/messaging-jms/

您可能需要进行一些挖掘才能使其按您想要的方式工作,但是我认为这种方法比您要使用的方法更具“高级”。