}
public static void main(String[] args)
{
// Create connection
Connection con = Client.createConnection();
try
{
con.connect("localhost", 5672, "test", "guest", "guest");
}
catch(Exception e)
{
System.out.print("Error connecting to broker");
e.printStackTrace();
}
// Create session
Session session = con.createSession(0);
// Create an instance of the listener
TopicListener listener = new TopicListener();
listener.prepareQueue(session,"usa", "usa.#");
listener.prepareQueue(session,"europe", "europe.#");
listener.prepareQueue(session,"news", "#.news");
listener.prepareQueue(session,"weather", "#.weather");
// confirm completion
session.sync();
// check to see if we have received all the messages
while (!listener.isFinished()){}
System.out.println("Shutting down listener for listener_destination");
listener.cancelSubscription(session,"usa");
listener.cancelSubscription(session,"europe");
listener.cancelSubscription(session,"news");
listener.cancelSubscription(session,"weather");
//cleanup
session.sessionDetach(session.getName());
try
{
con.close();
}
catch(Exception e)
{
System.out.print("Error closing broker connection");
e.printStackTrace();