Home > Back-end >  ActiveMQ. Advisory. The Connection can't listen to the Connection
ActiveMQ. Advisory. The Connection can't listen to the Connection

Time:09-28

This is a listener
 
Public static void main (String [] args) throws JMSException {
ConnectionFactory factory=new ActiveMQConnectionFactory (" manage ", "123456", "TCP://localhost: 61616");
The Connection Connection=factory. The createConnection ();
Connection. The start ();
The final Session Session=connection. CreateSession (false/* */support affairs, Session. AUTO_ACKNOWLEDGE);
Destination queue=AdvisorySupport. GetProducerAdvisoryTopic (session. CreateTopic (" & gt;" ));
MessageConsumer consumer=session. CreateConsumer (queue);

Consumer. SetMessageListener (new MessageListener () {
Public void onMessage (Message Message) {
System. The out. Println (message);
}
});
}


Activemq. XML is also configured the
 









Any subscription end
 
Public static void main (String [] args) throws JMSException, IOException {
//create a connection factory
ConnectionFactory ConnectionFactory=new ActiveMQConnectionFactory (" root ", "123456", "TCP://127.0.0.1:61616");
//create a connection
The Connection Connection=connectionFactory. The createConnection ();
//start
Connection. The start ();
//create a session object false indicates whether autocommit;
The Session Session=connection. The createSession (false, Session. AUTO_ACKNOWLEDGE);
//create a queue des subclass!
Topic Topic=session. CreateTopic (" test ");
//create the consumer
MessageConsumer MessageConsumer=session. CreateConsumer (topic);
MessageConsumer. SetMessageListener (new MessageListener () {
Public void onMessage (Message Message) {
TextMessage TextMessage=(TextMessage) message;
Try {
System. The out. Println (" PUB: "+ textMessage. GetText ());
} the catch (JMSException e) {
e.printStackTrace();
}
}
});
System. In the read ();
//close resources
MessageConsumer. Close ();
The session. The close ();
connection.close();
}



This subscription end when the listener can't listen to the SUB connection information

CodePudding user response:

Solved, should use AdvisorySupport. GetConnectionAdvisoryTopic (); This also get,,, can listen to disconnect and connect information, website sample invalid
 
Public static void main (String [] args) throws JMSException {
ConnectionFactory factory=new ActiveMQConnectionFactory (" manage ", "123456", "TCP://localhost: 61616");
The Connection Connection=factory. The createConnection ();
Connection. The start ();
The final Session Session=connection. CreateSession (false/* */support affairs, Session. AUTO_ACKNOWLEDGE);
ActiveMQTopic test=AdvisorySupport. GetConnectionAdvisoryTopic ();
MessageConsumer consumer=session. CreateConsumer (test);
Consumer. SetMessageListener (new MessageListener () {
Public void onMessage (Message Message) {
System. The out. Println (message);
}
});
}
  • Related