![](/img/trans.png)
[英]How to configure embedded ActiveMQ Broker URL with Spring Boot
[英]Spring embedded ActiveMQ, how to configure queue
我在Spring上下文中定義ActiveMQConnectionFactory
,並將其定義為Camel Component,如下所示
@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檢索隊列。
這是下面的示例,您需要通過定義代理bean將tcp Connector添加到代理。
默認情況下,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());
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.