简体   繁体   English

如何在Spring Boot应用程序中使用HornetMQ消息

[英]How to consume HornetMQ message in spring boot application

I am trying to integrate HornetMQ Consumer in Springboot application. 我正在尝试将HornetMQ Consumer集成到Springboot应用程序中。 I have seen different example but all of them are pointing ActiveMQ implementation which make me little bit confuse. 我看到了不同的示例,但是所有示例都指向ActiveMQ实现,这使我有些困惑。 I have written a standard HornetQ Consumer in java. 我已经用Java编写了一个标准的HornetQ Consumer。 Here is a code: 这是一个代码:

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);
        }
    }
}

This one is working fine but is there any standard way to write Message Consumer in spring boot application or should i directly create a bean of this client and use in Springboot application 这个工作正常,但是有什么标准方法可以在Spring Boot应用程序中编写Message Consumer,或者我应该直接创建此客户端的Bean并在Springboot应用程序中使用

--------------- hornetq-jms.xml--------- --------------- 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>

You could perhaps use Spring JMS and JmsTemplate for this. 您可能为此使用Spring JMS和JmsTemplate The default set up for Spring boot is using an ActiveMQ connection factory, but if you exchange this for a HornetQConnetionFactory, you should be good to go: 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;
    }
}

I added the fully qualified class names for clearity. 为了清楚起见,我添加了全限定类名。

Then, in some bean, you can just do this to consume a message: 然后,在某些bean中,您可以执行以下操作以消耗一条消息:

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

Disclaimer: I have not actually tried this in practice, it is based on my experience with Spring and ActiveMQ, as well as these sources: https://dzone.com/articles/connecting-spring https://spring.io/guides/gs/messaging-jms/ 免责声明:我实际上并没有尝试过此操作,它基于我对Spring和ActiveMQ的经验以及以下来源: https : //dzone.com/articles/connecting-spring https://spring.io/guides / GS /消息JMS /

You might have to do some digging to get this to work the way you want, but I think this approach is a bit more "high-level" than the one you are going for. 您可能需要进行一些挖掘才能使此方法按您想要的方式工作,但是我认为这种方法比您要使用的方法更具“高级”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Spring Boot 应用程序中消费或调用另一个 Rest API? - How to consume or call another Rest API in Spring Boot application? 如何在 Spring Boot 应用程序中使用 spring-rabbit 处理 JSON 消息? - How to deal with JSON message with spring-rabbit in spring boot application? 如何在 spring 启动中使用临时 AWS 凭证? - How to consume temporary AWS credentials in spring boot? 尝试使用 Spring 引导使用 JMS 主题消息时出现异常 - Exception while trying to consume a JMS Topic message using Spring Boot 如何在Spring Boot中使用数组中的数组? - how to consume an array within an array in spring boot? 如何在通过Helm部署的Java Spring启动应用程序中使用configmaps中的属性 - How to consume properties from configmaps in Java Spring boot application deployed through Helm 如何在Spring Boot中使用HTTPS GET服务 - How to consume a HTTPS GET service with Spring Boot 如何在 Spring 引导中使用外部 API - How to consume an external API in Spring Boot 如何使用 spring 引导应用程序在同一应用程序中使用 rest api 来自同一应用程序中的另一个模块的一个模块 - how can I consume rest api of one module from other module in same application using spring boot application without module dependency 如何在Spring Boot中使用带有休息模板的多部分表单数据 - How to consume multipart form data with a rest template in spring boot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM