/** * Returns true if key resolves to a *non-null value* (considering * a possible default), false otherwise. */ public boolean has(T key) { return get(key) != null; }
@Override public Listener respond(final MessageHandler handler, final Codecs codecs, Map<ListenOption, Object> options) throws Exception { final Options<ListenOption> opts = new Options<>(options); String selector = JMSMessage.SYNC_PROPERTY + " = TRUE"; if (opts.has(ListenOption.SELECTOR)) { selector += " AND " + opts.getString(ListenOption.SELECTOR); } opts.put(ListenOption.SELECTOR, selector); MessageHandler wrappedHandler = new MessageHandler() { @Override public Reply onMessage(Message msg, Context context) throws Exception { Reply result = handler.onMessage(msg, context); Options<MessageOpOption> replyOptions = new Options<>(); replyOptions.put(PublishOption.TTL, opts.getInt(RespondOption.TTL)); replyOptions.put(PublishOption.CONTEXT, context); replyOptions.put(PublishOption.PROPERTIES, result.properties()); ((ReplyableMessage)msg).reply(result.content(), codecs.forContentType(msg.contentType()), replyOptions); return null; } }; return listen(wrappedHandler, codecs, opts); }
public QuartzScheduling(String name, Options<CreateOption> options) { this.name = name; this.numThreads = options.getInt(CreateOption.NUM_THREADS); }
@Override public Object call() throws Exception { Connection connection; if (options.has(CreateContextOption.USERNAME)) { connection = cf.createConnection(options.getString(CreateContextOption.USERNAME), options.getString(CreateContextOption.PASSWORD)); } else { connection = cf.createConnection(); } return connection; } });
protected Options<CreateOption> validate(Map<CreateOption,Object> options) { Options<CreateOption> result = new Options<CreateOption>(options); String mode = result.getString(CreateOption.MODE); if (mode != null && !"LOCAL".equalsIgnoreCase(mode)) { log.warn("Requested mode only available in a cluster, setting to LOCAL"); result.put(CreateOption.MODE, "LOCAL"); } return result; }
throw new IllegalArgumentException("codec can't be null"); Options<MessageOpOption> opts = new Options<>(options); JMSSpecificContext context = context(opts.get(MessageOpOption.CONTEXT)); Session session = context.jmsSession(); javax.jms.Destination destination = jmsDestination(); fillInProperties(jmsMessage, (Map<String, Object>) opts.get(PublishOption.PROPERTIES, Collections.emptyMap())); fillInProperties(jmsMessage, additionalProperties); jmsMessage.setStringProperty(JMSMessage.CONTENT_TYPE_PROPERTY, codec.contentType()); opts.getBoolean(PublishOption.PERSISTENT) ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT, opts.getInt(PublishOption.PRIORITY), opts.getLong(PublishOption.TTL, producer.getTimeToLive())); } finally { producer.close();
protected boolean registerHandler(HttpHandler httpHandler, Map<RegisterOption, Object> opts, Runnable cleanup) { final Options<RegisterOption> options = new Options<>(opts); final String context = options.getString(RegisterOption.PATH); httpHandler = wrapWithSessionHandler(httpHandler); if (options.has(RegisterOption.STATIC_DIR)) { httpHandler = wrapWithStaticHandler(httpHandler, options.getString(RegisterOption.STATIC_DIR)); } if (options.getBoolean(RegisterOption.DISPATCH)) { httpHandler = wrapWithDispatcher(httpHandler); } final boolean replacement = pathology.add(context, options.getList(RegisterOption.VHOSTS), httpHandler); if (cleanup != null) { pathology.epilogue(httpHandler, cleanup); } if (autoStart) { start(); } log.info("Registered web context {}", context); return replacement; }
@Override public synchronized Queue findOrCreateQueue(String name, Map<CreateQueueOption, Object> options) throws Exception { Options<CreateQueueOption> opts = new Options<>(options); javax.jms.Queue queue; JMSSpecificContext givenContext = (JMSSpecificContext)opts.get(CreateQueueOption.CONTEXT); if (givenContext != null) { if (!givenContext.isRemote()) { throw new IllegalArgumentException("Queue lookup only accepts a remote context."); if (opts.size() > 1) { throw new IllegalArgumentException("Creation options provided for a remote queue."); } else { queue = createQueue(name, opts.getString(CreateQueueOption.SELECTOR, ""), opts.getBoolean(CreateQueueOption.DURABLE)); addCreatedDestination(DestinationUtil.fullName(name, JMSDestination.Type.QUEUE)); if (opts.size() > 0) { log.warn("Ignoring the queue creation options provided for " + name + ", the queue already exists.");
@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(); } }; }
@Override public Message receive(Codecs codecs, Map<MessageOpOption, Object> options) throws Exception { Options<MessageOpOption> opts = new Options<>(options); 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); MessageConsumer consumer = jmsSession.createConsumer(destination, selector);
private void configure(Options<CreateOption> options) { autoStart = options.getBoolean(CreateOption.AUTO_START); Undertow.Builder builder = (Undertow.Builder) options.get(CreateOption.CONFIGURATION); if (builder != null) { undertow = builder .setHandler(Handlers.header(pathology.handler(), Headers.SERVER_STRING, "undertow")) .build(); } else { int port = options.getInt(CreateOption.PORT); String host = options.getString(CreateOption.HOST); undertow = Undertow.builder() .addHttpListener(port, host) .setHandler(Handlers.header(pathology.handler(), Headers.SERVER_STRING, "undertow")) .build(); } }
protected ConnectionFactory createRemoteConnectionFactory(final Options<CreateContextOption> options) { //TODO: possibly cache the remote cf's? Map<String, Object> transportOpts = new HashMap<>(); transportOpts.put("host", options.getString(CreateContextOption.HOST)); transportOpts.put("port", options.getInt(CreateContextOption.PORT)); if (REMOTE_TYPE_WILDFLY.equals(options.getString(CreateContextOption.REMOTE_TYPE))) { transportOpts.put("http-upgrade-enabled", true); } TransportConfiguration config = new TransportConfiguration("org.hornetq.core.remoting.impl.netty.NettyConnectorFactory", transportOpts); HornetQConnectionFactory hornetQcf = HornetQJMSClient .createConnectionFactoryWithoutHA(options.has(CreateContextOption.XA) ? JMSFactoryType.XA_CF : JMSFactoryType.CF, config); hornetQcf.setReconnectAttempts(options.getInt(CreateContextOption.RECONNECT_ATTEMPTS)); hornetQcf.setRetryInterval(options.getLong(CreateContextOption.RECONNECT_RETRY_INTERVAL)); hornetQcf.setRetryIntervalMultiplier(options.getDouble(CreateContextOption.RECONNECT_RETRY_INTERVAL_MULTIPLIER)); hornetQcf.setMaxRetryInterval(options.getLong(CreateContextOption.RECONNECT_MAX_RETRY_INTERVAL)); // We have to cast for HornetQ 2.3 - the factory object that is returned is both a HornetQConnectionFactory // and a javax.jms.ConnectionFactory, but HornetQConnectionFactory doesn't implement j.j.ConnectionFactory. // With HornetQ 2.4, this cast is redundant. return (ConnectionFactory)hornetQcf; }
protected Trigger initTrigger(String name, Options<ScheduleOption> opts) { TriggerBuilder<Trigger> builder = TriggerBuilder.newTrigger() .withIdentity(name, name()); if (opts.has(AT)) { builder.startAt(opts.getDate(AT)); } else if (opts.has(IN)) { builder.startAt(new java.util.Date(System.currentTimeMillis() + opts.getLong(IN))); } else { builder.startNow(); } if (opts.has(UNTIL)) { builder.endAt(opts.getDate(UNTIL)); } if (opts.has(CRON)) { builder.withSchedule(CronScheduleBuilder.cronSchedule(opts.getString(CRON))); } else if (opts.has(EVERY)) { SimpleScheduleBuilder schedule = SimpleScheduleBuilder.simpleSchedule() .withIntervalInMilliseconds(opts.getInt(EVERY)); if (opts.has(LIMIT)) { schedule.withRepeatCount(opts.getInt(LIMIT) - 1); } else { schedule.repeatForever(); } builder.withSchedule(schedule); } return builder.build(); }
protected Options<MessageOpOption> replyOptions(Map<Destination.MessageOpOption, Object> options) throws Exception { Options<MessageOpOption> opts = new Options<>(options); Map<String, Object> properties = (Map<String, Object>)opts.get(Destination.PublishOption.PROPERTIES); Map<String, Object> newProperties = new HashMap<>(); if (properties != null) { newProperties.putAll(properties); } newProperties.put(SYNC_RESPONSE_PROPERTY, true); newProperties.put(REQUEST_ID_PROPERTY, requestID()); newProperties.put(REQUEST_NODE_ID_PROPERTY, requestNodeID()); opts.put(Destination.PublishOption.PROPERTIES, newProperties); return opts; }
@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 Reply onMessage(Message msg, Context context) throws Exception { Reply result = handler.onMessage(msg, context); Options<MessageOpOption> replyOptions = new Options<>(); replyOptions.put(PublishOption.TTL, opts.getInt(RespondOption.TTL)); replyOptions.put(PublishOption.CONTEXT, context); replyOptions.put(PublishOption.PROPERTIES, result.properties()); ((ReplyableMessage)msg).reply(result.content(), codecs.forContentType(msg.contentType()), replyOptions); return null; } };
@Override public synchronized Topic findOrCreateTopic(String name, Map<CreateTopicOption, Object> options) throws Exception { Options<CreateTopicOption> opts = new Options<>(options); javax.jms.Topic topic; JMSSpecificContext givenContext = (JMSSpecificContext)opts.get(CreateTopicOption.CONTEXT); if (givenContext != null) { if (!givenContext.isRemote()) { throw new IllegalArgumentException("Topic lookup only accepts a remote context."); if (opts.size() > 1) { throw new IllegalArgumentException("Creation options provided for a remote topic."); if (opts.size() > 0) { log.warn("Ignoring the topic creation options provided for " + name + ", the topic already exists.");
protected JMSSpecificContext createContext(final ConnectionFactory cf, final Options<CreateContextOption> options) { Connection connection = (Connection) DestinationUtil.mightThrow(new Callable() { @Override public Object call() throws Exception { Connection connection; if (options.has(CreateContextOption.USERNAME)) { connection = cf.createConnection(options.getString(CreateContextOption.USERNAME), options.getString(CreateContextOption.PASSWORD)); } else { connection = cf.createConnection(); } return connection; } }); return new JMSContext(connection, this, (Context.Mode)options.get(CreateContextOption.MODE), options.has(CreateContextOption.HOST)); }