Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 35447200CED for ; Fri, 18 Aug 2017 20:49:42 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 33A9816D447; Fri, 18 Aug 2017 18:49:42 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 5114016D440 for ; Fri, 18 Aug 2017 20:49:41 +0200 (CEST) Received: (qmail 69394 invoked by uid 500); 18 Aug 2017 18:49:40 -0000 Mailing-List: contact users-help@activemq.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: users@activemq.apache.org Delivered-To: mailing list users@activemq.apache.org Received: (qmail 69383 invoked by uid 99); 18 Aug 2017 18:49:39 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd2-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 18 Aug 2017 18:49:39 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd2-us-west.apache.org (ASF Mail Server at spamd2-us-west.apache.org) with ESMTP id 5F9A81A04DA for ; Fri, 18 Aug 2017 18:49:39 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd2-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 3.793 X-Spam-Level: *** X-Spam-Status: No, score=3.793 tagged_above=-999 required=6.31 tests=[HTML_MESSAGE=2, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, RCVD_IN_SORBS_SPAM=0.5, SPF_PASS=-0.001, URI_HEX=1.313, URI_TRY_3LD=0.001] autolearn=disabled Received: from mx1-lw-eu.apache.org ([10.40.0.8]) by localhost (spamd2-us-west.apache.org [10.40.0.9]) (amavisd-new, port 10024) with ESMTP id NkQNvXSakQnh for ; Fri, 18 Aug 2017 18:49:35 +0000 (UTC) Received: from mail-oi0-f42.google.com (mail-oi0-f42.google.com [209.85.218.42]) by mx1-lw-eu.apache.org (ASF Mail Server at mx1-lw-eu.apache.org) with ESMTPS id 9A9265F3D0 for ; Fri, 18 Aug 2017 18:49:34 +0000 (UTC) Received: by mail-oi0-f42.google.com with SMTP id f11so105264746oic.0 for ; Fri, 18 Aug 2017 11:49:34 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to; bh=cHrLOyypf7AUOLYfB1We9OQXcmXfS/Mvt20cupawxAY=; b=fyw61cgdUdiS8tTNWMom5KTHRzludZ1VTdlVoWMzSfJ3oLc8MXsxLoBy+d5Zb4HaeP YWRLaXa45eyukvjM4HV/RSllekFQsucaGswdbrBNz+wPWS5/A/kAbiONXDIJ7BvyxaDq gPss7nbarUZISpw6td9/1+jh6zlYb+ANJALLR+Pr22ikQXSyIobFUxwazP4sp4Bw6mNK WYeS/TanaaP/l/Yo9lHspksAaTRfoJ4CLaQ2+OHfzYdZNIR/Y2/0kPFKmRfV44lZWN2U 3cMUhHfJYO8ApFJ+WETqYP+i/yJcfXSne4O0UywdrV/QwXazO6nmpQ11/cevODtbZl7b Nx/A== X-Gm-Message-State: AHYfb5hzADzP1cqea4dh1PiPlGMXuL6EJt8qPH02/SqSHyOw/uF9zJow 7Lpq2GfbMcWkx4nvoDw9JMxnFJ0g7SGT9Nk= X-Received: by 10.202.225.67 with SMTP id y64mr13335745oig.159.1503082173096; Fri, 18 Aug 2017 11:49:33 -0700 (PDT) MIME-Version: 1.0 Received: by 10.74.171.131 with HTTP; Fri, 18 Aug 2017 11:49:32 -0700 (PDT) In-Reply-To: <1503079193215-4729816.post@n4.nabble.com> References: <1503077176934-4729814.post@n4.nabble.com> <1503079193215-4729816.post@n4.nabble.com> From: Justin Bertram Date: Fri, 18 Aug 2017 13:49:32 -0500 Message-ID: Subject: Re: Close message listener decoupled To: users@activemq.apache.org Content-Type: multipart/alternative; boundary="001a113d4d6efefaeb05570b96f8" archived-at: Fri, 18 Aug 2017 18:49:42 -0000 --001a113d4d6efefaeb05570b96f8 Content-Type: text/plain; charset="UTF-8" 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 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 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. > --001a113d4d6efefaeb05570b96f8--