protected void validateOptions(Options<ScheduleOption> opts) throws IllegalArgumentException { if (opts.has(CRON)) { for(ScheduleOption each : new ScheduleOption[] {EVERY, LIMIT}) { if (opts.has(each)) { throw new IllegalArgumentException("You can't specify both 'cron' and '" + each.name + "'"); } } } if (opts.has(AT) && opts.has(IN)) { throw new IllegalArgumentException("You can't specify both 'at' and 'in'"); } if (!opts.has(EVERY)) { if (opts.has(LIMIT)) { throw new IllegalArgumentException("You can't specify 'limit' without 'every'"); } if (opts.has(UNTIL) && !opts.has(CRON)) { throw new IllegalArgumentException("You can't specify 'until' without 'every' or 'cron'"); } } }
@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 JMSSpecificContext createXAContext(final XAConnectionFactory cf, final Options<CreateContextOption> options) { if (TransactionUtil.tm == null) { throw new NullPointerException("TransactionManager not found; is transactions module on the classpath?"); } XAConnection connection = (XAConnection) DestinationUtil.mightThrow(new Callable() { @Override public Object call() throws Exception { if (options.has(CreateContextOption.USERNAME)) { return cf.createXAConnection(options.getString(CreateContextOption.USERNAME), options.getString(CreateContextOption.PASSWORD)); } else { return cf.createXAConnection(); } } }); return new JMSXAContext(connection, this, (Context.Mode)options.get(CreateContextOption.MODE), options.has(CreateContextOption.HOST)); }
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 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)); }
@Override public Object call() throws Exception { if (options.has(CreateContextOption.USERNAME)) { return cf.createXAConnection(options.getString(CreateContextOption.USERNAME), options.getString(CreateContextOption.PASSWORD)); } else { return cf.createXAConnection(); } } });
@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); }
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; }
@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; }
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; }