activemq-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Justin Bertram <jbert...@redhat.com>
Subject Re: Close message listener decoupled
Date Fri, 18 Aug 2017 18:49:32 GMT
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.

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.herbold@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.
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message