@Override public Listener listen(final MessageHandler handler, Codecs codecs, Map<ListenOption, Object> options) throws Exception { Options<ListenOption> opts = new Options<>(options); Context givenContext = (Context)opts.get(ListenOption.CONTEXT); if (givenContext != null && !givenContext.isRemote()) { throw new IllegalArgumentException("Listening only accepts a remote context."); } MessageHandler wrappedHandler = new MessageHandler() { @Override public Reply onMessage(Message msg, Context ctx) throws Exception { ((JMSSpecificContext)ctx).setLatestMessage((JMSMessage)msg); return handler.onMessage(msg, ctx); } }; final JMSSpecificContext context = context(givenContext); Listener listener = new JMSMessageHandlerGroup(context, wrappedHandler, codecs, this, opts).start(); if (givenContext != null) { givenContext.addCloseable(listener); } this.broker.addCloseableForDestination(this, listener); this.broker.addCloseable(listener); return listener; }
@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(); } }; }