在关闭应用服务器时,在调用ActiveMQ连接的close()方法之前,先调用disconnect()方法。这样可以保证ActiveMQ连接正确关闭,避免阻止应用服务器停止。
示例代码:
import javax.jms.Connection;
import org.apache.activemq.ActiveMQConnectionFactory;
public class ActiveMQDemo {
private Connection connection;
private ActiveMQConnectionFactory factory;
public void connect() {
try {
factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
connection = factory.createConnection();
connection.start();
} catch (Exception e) {
e.printStackTrace();
}
}
public void disconnect() {
try {
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
ActiveMQDemo activeMQDemo = new ActiveMQDemo();
activeMQDemo.connect();
//业务逻辑代码...
activeMQDemo.disconnect();
}
}