我是ActiveMQ的新手,我需要创建spring boot aplication,其中publis activeMQ队列。因此我创建了简单的SpringBoot应用程序
@SpringBootApplication
@EnableJms
public class Application {
@Bean
JmsListenerContainerFactory<?> myJmsContainerFactory(ConnectionFactory connectionFactory) {
SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
return factory;
}
public static void main(String[] args) {
FileSystemUtils.deleteRecursively(new File("activemq-data"));
// Launch the application
ApplicationContext context = SpringApplication.run(Application.class, args);
System.out.println(" ************************ Asyn queue start ************************ ");
}
}
然后我也创建了一个Listneres:
@Component
public class Receiver {
@Autowired
ConfigurableApplicationContext context;
@JmsListener(destination = "mailbox-destination", containerFactory = "myJmsContainerFactory")
public void receiveMessageFromMailbox(String message) {
System.out.println("Received <" + message + "> mailbox");
//context.close();
FileSystemUtils.deleteRecursively(new File("activemq-data"));
}
@JmsListener(destination = "testqueue-destination", containerFactory = "myJmsContainerFactory")
public void receiveMessageFromTestQueue(String message) {
System.out.println("Received <" + message + "> testqueue");
//context.close();
FileSystemUtils.deleteRecursively(new File("activemq-data"));
}
}
启动后,看起来没问题。
为了测试,我创建了一个简单的测试:
public class Testing {
public static void main(String[] args) throws JMSException {
try {
// Create a ConnectionFactory
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost");
// Create a Connection
Connection connection = connectionFactory.createConnection();
connection.start();
// Create a Session
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Create the destination (Topic or Queue)
Destination destination = session.createQueue("testqueue-destination");
// Create a MessageProducer from the Session to the Topic or Queue
MessageProducer producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
// Create a messages
String text = "Hello world! From: " + Thread.currentThread().getName() + " : ";
TextMessage message = session.createTextMessage(text);
// Tell the producer to send the message
System.out.println("Sent message: "+ message.hashCode() + " : " + Thread.currentThread().getName() + " : " + message);
producer.send(message);
// Clean up
session.close();
connection.close();
} catch (Exception e) {
System.out.println("Caught: " + e);
e.printStackTrace();
}
}
}
但是当我运行Testing.main时,会发生错误:
[main] DEBUG org.apache.activemq.broker.jmx.ManagementContext - 无法创建本地注册表 java.rmi.server.ExportException:端口已在使用中:1099;嵌套异常是: java.net.BindException:已在使用的地址:JVM_Bind at sun.rmi.transport.tcp.TCPTransport.listen(TCPTransport.java:341) at sun.rmi.transport.tcp.TCPTransport.exportObject(TCPTransport.java:249) at sun.rmi.transport.tcp.TCPEndpoint.exportObject(TCPEndpoint.java:411) at sun.rmi.transport.LiveRef.exportObject(LiveRef.java:147) at sun.rmi.server.UnicastServerRef.exportObject(UnicastServerRef.java:208) at sun.rmi.registry.RegistryImpl.setup(RegistryImpl.java:152) at sun.rmi.registry.RegistryImpl。(RegistryImpl.java:112) 在java.rmi.registry.LocateRegistry.createRegistry(LocateRegistry.java:239) 在org.apache.activemq.broker.jmx.ManagementContext.createConnector(ManagementContext.java:418) at org.apache.activemq.broker.jmx.ManagementContext.findTigerMBeanServer(ManagementContext.java:363) 在org.apache.activemq.broker.jmx.ManagementContext.findMBeanServer(ManagementContext.java:330) 在org.apache.activemq.broker.jmx.ManagementContext.getMBeanServer(ManagementContext.java:172) 在org.apache.activemq.broker.jmx.ManagementContext.start(ManagementContext.java:80) 在org.apache.activemq.broker.BrokerService.startManagementContext(BrokerService.java:2031) 在org.apache.activemq.broker.BrokerService.start(BrokerService.java:477)
我认为问题出在测试方法上。但我不知道什么是不正确的。有人可以帮助我吗?感谢。
答案 0 :(得分:0)
您是否尝试运行netstart命令来检查端口是否正在使用?
对于linux netstat -pnlt | grep 1099
对于windows netstat -np TCP |找到“1099”
答案 1 :(得分:0)
我知道SpringBoot应用程序正在使用该端口。 SpringBoot应用程序启动后,将打印以下日志:
[ main] o.apache.activemq.broker.BrokerService : Using Persistence Adapter: MemoryPersistenceAdapter
[ main] o.apache.activemq.broker.BrokerService : ActiveMQ 5.4.2 JMS Message Broker (localhost) is starting
[ main] o.apache.activemq.broker.BrokerService : For help or more information please see: http://activemq.apache.org/
[ JMX connector] o.a.a.broker.jmx.ManagementContext : JMX consoles can connect to service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi
[ main] o.apache.activemq.broker.BrokerService : ActiveMQ JMS Message Broker (localhost, ID:CZ408016-49987-1470397291692-0:1) started
[ main] o.a.activemq.broker.TransportConnector : Connector vm://localhost Started
[ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
[ main] o.s.c.support.DefaultLifecycleProcessor : Starting beans in phase 2147483647
[ main] c.c.e.asynqueue.spring.Application : Started Application in 3.117 seconds (JVM running for 4.52)
但我假设我可以通过以下代码连接到经纪人:
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost");
// Create a Connection
Connection connection = connectionFactory.createConnection();
connection.start();