public JMSListener(MessageHandler handler, Codecs codecs, Destination endpoint, JMSSpecificContext context, MessageConsumer consumer) { this.codecs = codecs; this.endpoint = endpoint; this.handler = handler; this.context = context; this.consumer = consumer; addCloseable(consumer); addCloseable(context); }
public void stop() { if (this.started) { try { closeCloseables(); } catch (Exception e) { log.error("Failed to stop handler: ", e); } this.started = false; } }
public JMSListener start() { start(new Runnable() { @Override public void run() { try { consumer.setMessageListener(JMSListener.this); } catch (Exception e) { log.error("Failed to start handler: ", e); } } }); return this; }
@Override public Listener createListener(final MessageHandler handler, final Codecs codecs, final Destination destination, final Context context, final Options<Destination.ListenOption> options) throws Exception { JMSSpecificContext subContext = ((JMSSpecificContext)context) .createChildContext((Context.Mode) options.get(Destination.ListenOption.MODE)); return new JMSListener(handler, codecs, destination, subContext, createConsumer(destination, subContext, options)).start(); }
@Override public void close() { stop(); }
@Override public Listener subscribe(final String id, final MessageHandler handler, final Codecs codecs, final Map<SubscribeOption, Object> options) throws Exception { Options<SubscribeOption> opts = new Options<>(options); final JMSSpecificContext context = (JMSSpecificContext)context(id, opts.get(SubscribeOption.CONTEXT)); final TopicSubscriber subscriber = context .jmsSession() .createDurableSubscriber((javax.jms.Topic) jmsDestination(), id, opts.getString(SubscribeOption.SELECTOR), false); final Listener listener = new JMSListener(handler, codecs, this, context, subscriber).start(); Context parent = (Context)opts.get(SubscribeOption.CONTEXT); if (parent != null) { parent.addCloseable(listener); } broker().addCloseableForDestination(this, listener); return new Listener() { @Override public void close() throws Exception { listener.close(); context.close(); } }; }