On 08/18/2017 02:49 PM, Justin Bertram wrote:
I see a couple of potential problems with this code:

1) It's littered with non-JMS code (e.g. ActiveMQConnectionFactory,
ActiveMQConnection).  You may not care about that if this application will
only ever integrate with ActiveMQ, but if you think you'll ever want to use
any other kind of JMS broker then you'll have to rewrite some of this stuff
when you migrate.  APIs are standardized for a reason.  Deviate at your own
risk.

I can't see any reason why anyone would ever not want to use ActiveMQ  ;)


2) Every time you invoke register() you create a new Connection, Session,
Consumer, etc.  This is an anti-pattern.  Connection objects are "heavy"
and are meant to be shared.

3) You do not keep track of any of the resources you create (i.e.
Connection, Session, Consumer) so you have no way to clean them up.

  > maybe the strcture is not good at all

I'd agree with that.


Justin

On Fri, Aug 18, 2017 at 12:59 PM, Juleian <julian.herb...@gmx.net> wrote:

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.


--
Tim Bish
twitter: @tabish121
blog: http://timbish.blogspot.com/

Reply via email to