That sounds reasonable actually. The only problem I also somehow have is when register is called the method "is stuck" in the thread that is opened or not? I do not see how I can return a value with this structure (maybe the strcture is not good at all, its my first real programming task).
public register(String topicName) {
try {
// create connection to Broker, create Session and
Consumer
ActiveMQConnectionFactory connectionFactory = new
ActiveMQConnectionFactory(
"tcp://localhost:61616");
ActiveMQConnection connection = (ActiveMQConnection)
connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
Topic topicObject = session.createTopic(topicName);
MessageConsumer consumer =
session.createConsumer(topicObject);
//check if topic is available on Broker
DestinationSource ds =
connection.getDestinationSource();
Set<ActiveMQTopic> topics = ds.getTopics();
String compare = topicName;
int count = 0;
for(ActiveMQTopic topic : topics){
if(compare.equals(topic.getTopicName())) {
System.out.println("Found " +
topic.getTopicName());
count = count + 1;
}
}
if(count == 0){
System.out.println("The topic you want to
subscribe to is not
found.");
System.out.println("Please try again with a
valid topic name.");
return;
}
//register Component at mongoDB
MessageListener listner = new MessageListener() {
@Override
public void onMessage(Message message) {
try {
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
System.out.println("Received message : "
+ textMessage.getText() + "'");
}
} catch (JMSException e) {
System.out.println("Caught:" + e);
}
}
};
consumer.setMessageListener(listner);
try {
System.in.read();
} catch (IOException e) {
}
} catch (JMSException ex) {
// Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE,
null, ex);
}
}
--
View this message in context:
http://activemq.2283324.n4.nabble.com/Close-message-listener-decoupled-tp4729814p4729816.html
Sent from the ActiveMQ - User mailing list archive at Nabble.com.
