public JMSContext(final Connection jmsConnection, final Messaging broker, final Mode mode, final boolean remote, final JMSSpecificContext parent) { this.mode = mode; this.id = UUID.randomUUID().toString(); this.parentContext = parent; this.broker = broker; this.remote = remote; this.jmsConnection = jmsConnection; if (parentContext != null) { parentContext.addCloseable(this); } else { addCloseable(jmsConnection); } }
@Override public void onMessage(Message message) { try { JMSContext.currentContext.set(this.context.asNonCloseable()); this.handler.onMessage(new JMSMessage(message, this.codecs.forContentType(JMSMessage.contentType(message)), this.endpoint), this.context); this.context.commit(); } catch (Throwable e) { this.context.rollback(); throw new RuntimeException("Unexpected error handling message", e); } finally { JMSContext.currentContext.remove(); } }
@Override public void close() throws Exception { listener.close(); context.close(); } };
int timeout = opts.getInt(ReceiveOption.TIMEOUT); JMSSpecificContext context = context(opts.get(MessageOpOption.CONTEXT)); Session jmsSession = context.jmsSession(); javax.jms.Destination destination = jmsDestination(); String selector = opts.getString(ReceiveOption.SELECTOR); context.setLatestMessage(wrappedMessage); context.close();
Session session = context.jmsSession(); javax.jms.Destination destination = jmsDestination(); MessageProducer producer = session.createProducer(destination); } finally { producer.close(); context.close();
@Override public Object call() throws Exception { return context.jmsSession().createQueue(name); } });
@Override public Response request(Object content, Codec codec, Codecs codecs, Map<MessageOpOption, Object> options) throws Exception { final Options<MessageOpOption> opts = new Options<>(options); final String id = UUID.randomUUID().toString(); final JMSSpecificContext context = (JMSSpecificContext)opts.get(MessageOpOption.CONTEXT); final String nodeId = context != null ? context.id() : JMSMessagingSkeleton.BROKER_ID; final ConcreteResponse response = new ConcreteResponse(); Options<ListenOption> routerOpts = new Options<>(); routerOpts.put(ListenOption.CONCURRENCY, 1); routerOpts.put(ListenOption.SELECTOR, JMSMessage.REQUEST_NODE_ID_PROPERTY + " = '" + nodeId + "' AND " + JMSMessage.SYNC_RESPONSE_PROPERTY + " = TRUE"); if (context != null && context.isRemote()) { routerOpts.put(ListenOption.CONTEXT, context); } ResponseRouter.routerFor(this, codecs, routerOpts).registerResponse(id, response); publish(content, codec, options, new HashMap<String, Object>() {{ put(JMSMessage.REQUEST_NODE_ID_PROPERTY, nodeId); put(JMSMessage.SYNC_PROPERTY, true); put(JMSMessage.REQUEST_ID_PROPERTY, id); }}); return response; }
@Override public boolean isRemote() { return this.remote || (this.parentContext != null && this.parentContext.isRemote()); }
protected JMSSpecificContext context(final String id, final Object context) throws Exception { if (context != null) { return ((JMSSpecificContext)context).asNonCloseable(); } else { return (JMSSpecificContext)broker() .createContext(new HashMap<Messaging.CreateContextOption, Object>() {{ put(Messaging.CreateContextOption.CLIENT_ID, id); }}); } }
@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 Context createContext(Map<CreateContextOption, Object> options) throws Exception { final Options<CreateContextOption> opts = new Options<>(options); JMSSpecificContext context; boolean xa = opts.getBoolean(CreateContextOption.XA); ConnectionFactory cf; if (opts.has(CreateContextOption.HOST)) { cf = createRemoteConnectionFactory(opts); } else { start(); cf = (ConnectionFactory)lookupJNDI(xa ? JNDI_XA_CF_NAME : JNDI_CF_NAME); } if (xa) { context = createXAContext((XAConnectionFactory)cf, opts); } else { context = createContext(cf, opts); } if (opts.has(CreateContextOption.CLIENT_ID)) { context.jmsConnection().setClientID(opts.getString(CreateContextOption.CLIENT_ID)); } return context; }
@Override public Object call() throws Exception { return context.jmsSession().createTopic(name); } });
JMSSpecificContext givenContext = (JMSSpecificContext)opts.get(CreateTopicOption.CONTEXT); if (givenContext != null) { if (!givenContext.isRemote()) { throw new IllegalArgumentException("Topic lookup only accepts a remote context.");
protected JMSSpecificContext context(final Object context) throws Exception { Context newContext; JMSSpecificContext threadContext = JMSContext.currentContext.get(); if (context != null) { newContext = ((JMSSpecificContext) context).asNonCloseable(); } else if (TransactionUtil.isTransactionActive()) { newContext = this.broker.createContext(new HashMap() {{ put(Messaging.CreateContextOption.XA, true); }}); } else if (threadContext != null) { newContext = threadContext; } else { newContext = this.broker.createContext(null); } newContext.enlist(); return (JMSSpecificContext)newContext; }
protected MessageConsumer createConsumer(final Destination destination, final Context context, final Options<Destination.ListenOption> options) throws JMSException { return ((JMSSpecificContext)context) .jmsSession() .createConsumer(((JMSDestination) destination).jmsDestination(), options.getString(Destination.ListenOption.SELECTOR)); } }
JMSSpecificContext givenContext = (JMSSpecificContext)opts.get(CreateQueueOption.CONTEXT); if (givenContext != null) { if (!givenContext.isRemote()) { throw new IllegalArgumentException("Queue lookup only accepts a remote context.");
@Override public void unsubscribe(String id, Map<UnsubscribeOption, Object> options) throws Exception { final Options<UnsubscribeOption> opts = new Options<>(options); try (JMSSpecificContext context = (JMSSpecificContext)context(id, opts.get(UnsubscribeOption.CONTEXT))) { context.jmsSession().unsubscribe(id); } }
@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(); } }; }