【问题标题】:Spring embedded ActiveMQ, how to configure queueSpring内嵌ActiveMQ,如何配置队列
【发布时间】:2018-09-16 01:25:21
【问题描述】:

我在Spring上下文中定义ActiveMQConnectionFactory,定义为骆驼组件如下

@Bean
@Qualifier("jms1")
public JmsComponent jms1() {
    ConnectionFactory f = new ActiveMQConnectionFactory("vm://localhost:7777");
    return JmsComponent.jmsComponentAutoAcknowledge(f);
}

Spring 会自动在本地 VM 上启动 ActiveMQ JMS 服务器,监听 7777 端口。但是如何在服务器上配置命名队列呢?我想在任何我需要的地方@Autowire 队列,比如从 Java EE 世界的 JNDI 中检索它。

【问题讨论】:

标签: java spring apache-camel jms activemq


【解决方案1】:

下面是一个例子,你需要通过定义 broker bean 将 tcp Connector 添加到 broker。

AMQ 默认支持 jndi。

看看http://activemq.apache.org/jndi-support.html

import java.util.Properties;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.broker.BrokerService;
import org.apache.activemq.command.ActiveMQDestination;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.camel.component.jms.JmsComponent;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@SpringBootApplication
@Configuration
public class ActiveMQConfigurationWithJndi {
    public static final String DESTINATION_NAME = "TEST";

    public static void main(String[] args) {
        ConfigurableApplicationContext app = SpringApplication.run(ActiveMQConfigurationWithJndi.class, args);
        Connection conn = null;
        try {
            String factoryName = "ConnectionFactory";
            Properties props = new Properties();
            props.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.activemq.jndi.ActiveMQInitialContextFactory");
            props.put(Context.PROVIDER_URL, "tcp://localhost:61616");
            props.put("queue." + DESTINATION_NAME, DESTINATION_NAME);
            InitialContext ic = new InitialContext(props);
            ConnectionFactory cf = (ConnectionFactory) ic.lookup(factoryName);
            ActiveMQQueue dest = (ActiveMQQueue) ic.lookup(DESTINATION_NAME);
            conn = cf.createConnection();
            conn.start();
            Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
            MessageProducer producer = session.createProducer(dest);
            TextMessage message = session.createTextMessage("textt");
            producer.send(message);
            System.out.println("ok");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (Exception e) {
                }
            }
        }
    }

    @Bean
    public BrokerService broker() throws Exception {
        final BrokerService broker = new BrokerService();
        broker.addConnector("tcp://localhost:61616");
        broker.addConnector("vm://localhost:7777");
        broker.setPersistent(false);
        broker.setDestinations(new ActiveMQDestination[] { queue() });
        return broker;
    }

    @Bean
    public ActiveMQDestination queue() {
        return new ActiveMQQueue(DESTINATION_NAME);
    }

    @Bean
    public ConnectionFactory jmsConnectionFactory() {
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
        return connectionFactory;
    }

    @Bean
    @Qualifier("jms1")
    public JmsComponent jms1() {
        return JmsComponent.jmsComponentAutoAcknowledge(jmsConnectionFactory());
    }
}

【讨论】:

  • 您的示例显示了如何打开连接并发送电子邮件。 OP 请求是关于预配置队列的。
猜你喜欢
  • 2021-08-08
  • 2017-09-13
  • 1970-01-01
  • 2016-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-31
  • 2014-11-21
相关资源
最近更新 更多