@Override public void get(String sid, final Handler<JsonObject> callback) { sid = this.prefix + sid; JsonObject redis = new JsonObject(); redis.putString("command", "get"); redis.putArray("args", new JsonArray().add(sid)); eventBus.send( redisAddress, redis, new Handler<Message<JsonObject>>() { @Override public void handle(Message<JsonObject> reply) { if ("ok".equals(reply.body().getString("status"))) { String value = reply.body().getString("value"); if (value == null || "".equals(value)) { callback.handle(null); return; } callback.handle(new JsonObject(value)); } else { callback.handle(null); } } }); }
private void checkAndSend( boolean send, final String address, Object body, final SockJSSocket sock, final String replyAddress) { final Handler<Message> replyHandler; if (replyAddress != null) { replyHandler = new Handler<Message>() { public void handle(Message message) { // Note we don't check outbound matches for replies // Replies are always let through if the original message // was approved checkAddAccceptedReplyAddress(message.replyAddress); deliverMessage(sock, replyAddress, message); } }; } else { replyHandler = null; } if (log.isDebugEnabled()) { log.debug("Forwarding message to address " + address + " on event bus"); } if (send) { eb.send(address, body, replyHandler); } else { eb.publish(address, body); } }
@SuppressWarnings("unused") private void getKeys(final Handler<JsonArray> next) { JsonObject redis = new JsonObject(); redis.putString("command", "keys"); redis.putArray("args", new JsonArray().add(prefix + "*")); eventBus.send( redisAddress, redis, new Handler<Message<JsonObject>>() { @Override public void handle(Message<JsonObject> message) { if (!"ok".equals(message.body().getString("status"))) { next.handle(new JsonArray()); } else { JsonArray keys = message.body().getArray("value"); JsonArray result = new JsonArray(); int len = prefix.length(); for (Object o : keys) { String key = (String) o; result.add(key.substring(len)); } next.handle(result); } } }); }
/** * Destroys a session and gives the result to a handler. * * @param sessionId The id of the session to destroy. * @param doneHandler The handler to call with the result of the session manager. */ public void destroySession(String sessionId, final Handler<JsonObject> doneHandler) { eventBus.send( smAddress, new JsonObject().putString("action", "destroy").putString("sessionId", sessionId), new Handler<Message<JsonObject>>() { public void handle(Message<JsonObject> arg0) { doneHandler.handle(arg0.body); } }); }
/** * Gets informations about open sessions and delivers this info to the specified handler. * * @param handler The handler to call after getting the results of the connections report. */ public void withConnectionStats(final Handler<JsonObject> handler) { eventBus.send( smAddress, new JsonObject().putString("action", "status").putString("report", "connections"), new Handler<Message<JsonObject>>() { @Override public void handle(Message<JsonObject> event) { handler.handle(event.body); } }); }
private void sendPutData( String sessionId, JsonObject putObject, final Handler<JsonObject> doneHandler) { JsonObject json = new JsonObject() .putString("action", "put") .putString("sessionId", sessionId) .putObject("data", putObject); if (doneHandler != null) { eventBus.send( smAddress, json, new Handler<Message<JsonObject>>() { @Override public void handle(Message<JsonObject> msg) { doneHandler.handle(msg.body); } }); } else { eventBus.send(smAddress, json); } }
@Override public void all(final Handler<JsonArray> next) { JsonObject redis = new JsonObject(); redis.putString("command", "keys"); redis.putArray("args", new JsonArray().add(prefix + "*")); final JsonArray results = new JsonArray(); eventBus.send( redisAddress, redis, new Handler<Message<JsonObject>>() { @Override public void handle(Message<JsonObject> message) { if (!"ok".equals(message.body().getString("status"))) { next.handle(null); } else { JsonArray keys = message.body().getArray("value"); new AsyncIterator<Object>(keys.iterator()) { @Override public void handle(Object key) { if (hasNext()) { JsonObject redis = new JsonObject(); redis.putString("command", "get"); redis.putArray("args", new JsonArray().add(key)); eventBus.send( redisAddress, redis, new Handler<Message<JsonObject>>() { @Override public void handle(Message<JsonObject> message) { if (!"ok".equals(message.body().getString("status"))) { next.handle(null); } else { String value = message.body().getString("value"); if (value != null) { results.add(new JsonObject(value)); } next(); } } }); } else { next.handle(results); } } }; } } }); }
/** * Checks all sessions for a potential match of the specified JsonObject and returns the reply of * the session manager ({ matches : true/false, sessions: [...all matching sessions or empty * JsonArray...] }). * * @param json The JsonObject to check against all sessions. * @param doneHandler Handles the result of the session manager. */ public void checkAllSessionsForMatch(JsonObject json, final Handler<JsonObject> doneHandler) { eventBus.send( smAddress, new JsonObject() .putString("action", "status") .putString("report", "matches") .putObject("data", json), new Handler<Message<JsonObject>>() { public void handle(Message<JsonObject> arg0) { doneHandler.handle(arg0.body); }; }); }
/** * Gets multiple data fields from the session storage by using a session id. * * @param sessionId The id of the session. * @param requiredFields The wanted fields from the session storage. * @param handler A handler for the received data. */ public void withSessionData( String sessionId, JsonArray requiredFields, final Handler<JsonObject> handler) { final JsonObject json = new JsonObject() .putString("action", "get") .putString("sessionId", sessionId) .putArray("fields", requiredFields); eventBus.send( smAddress, json, new Handler<Message<JsonObject>>() { public void handle(Message<JsonObject> event) { handler.handle(event.body); }; }); }
public static void main(final String[] args) throws Exception { System.setProperty( "vertx.clusterManagerFactory", HazelcastClusterManagerFactory.class.getName()); PlatformManager platformManager = null; try { // Step 1 Create a Vert.x PlatformManager platformManager = PlatformLocator.factory.createPlatformManager(PORT, HOST); final CountDownLatch latch0 = new CountDownLatch(1); // Step 2 Deploy a Verticle to receive message String verticle = "org.apache.activemq.artemis.core.example.ExampleVerticle"; platformManager.deployVerticle( verticle, null, new URL[0], 1, null, new Handler<AsyncResult<String>>() { @Override public void handle(AsyncResult<String> result) { if (!result.succeeded()) { throw new RuntimeException("failed to deploy verticle", result.cause()); } latch0.countDown(); } }); latch0.await(); // Step 3 Send a message to the incoming connector's address EventBus bus = platformManager.vertx().eventBus(); bus.send(INCOMING, MSG); // Step 4 Waiting for the Verticle to process the message latch.await(10000, TimeUnit.MILLISECONDS); } finally { if (platformManager != null) { platformManager.undeployAll(null); platformManager.stop(); } reportResultAndExit(); } }
private void authorise( final JsonObject message, final String sessionID, final AsyncResultHandler<Boolean> handler) { // If session id is in local cache we'll consider them authorised final AsyncResult<Boolean> res = new AsyncResult<>(); if (authCache.containsKey(sessionID)) { res.setResult(true).setHandler(handler); } else { eb.send( authAddress, message, new Handler<Message<JsonObject>>() { public void handle(Message<JsonObject> reply) { boolean authed = reply.body.getString("status").equals("ok"); res.setResult(authed).setHandler(handler); } }); } }
/** * Creates a new session for the specified request. It will provide the newly created session id * as a cookie. * * @param req The http server request, i.e. client, to create a session for. * @param handlerWithSessionId A handler to use the created session id with. */ public void startSession( final HttpServerRequest req, final Handler<String> handlerWithSessionId) { // Create a new session and use that eventBus.send( smAddress, new JsonObject().putString("action", "start"), new Handler<Message<JsonObject>>() { @Override public void handle(Message<JsonObject> event) { String sessionId = event.body.getString("sessionId"); CookieEncoder ce = new CookieEncoder(true); ce.addCookie(cookieField, sessionId); req.response.putHeader("Set-Cookie", ce.encode()); if (handlerWithSessionId != null) { handlerWithSessionId.handle(sessionId); } } }); }
@Override @SuppressWarnings("rawtypes") public void flush() throws TTransportException { if (methodCall_.isOneway()) { eventBus_.send(address_, outputBuffer_); } else { eventBus_.sendWithTimeout( address_, outputBuffer_, timeout_, new Handler<AsyncResult<Message<Buffer>>>() { @Override public void handle(AsyncResult<Message<Buffer>> event) { if (event.failed()) { methodCall_.onError( new TTransportException( TTransportException.TIMED_OUT, "No reply after " + timeout_ + "ms for method call of seqid: " + methodCall_.getSeqId(), event.cause())); } else { Message message = event.result(); Object body = message.body(); if (!(body instanceof Buffer)) { methodCall_.onError( new TProtocolException( TProtocolException.INVALID_DATA, "Reply message type not supported: " + message.getClass().getName())); return; } responseHandler_.handleResponse( new TMemoryInputTransport(((Buffer) body).getBytes())); } } }); } // Waiting for the next request resetOutputBuffer(); }
@Override public void length(final Handler<Integer> next) { JsonObject redis = new JsonObject(); redis.putString("command", "keys"); redis.putArray("args", new JsonArray().add(prefix + "*")); eventBus.send( redisAddress, redis, new Handler<Message<JsonObject>>() { @Override public void handle(Message<JsonObject> message) { if (!"ok".equals(message.body().getString("status"))) { next.handle(0); } else { JsonArray keys = message.body().getArray("value"); next.handle(keys.size()); } } }); }
@Override public void destroy(String sid, final Handler<Object> callback) { sid = this.prefix + sid; JsonObject redis = new JsonObject(); redis.putString("command", "del"); redis.putArray("args", new JsonArray().add(sid)); eventBus.send( redisAddress, redis, new Handler<Message<JsonObject>>() { @Override public void handle(Message<JsonObject> reply) { if ("ok".equals(reply.body().getString("status"))) { callback.handle(null); } else { callback.handle(reply.body().getString("message")); } } }); }
@Override public void set(String sid, JsonObject sess, final Handler<Object> callback) { sid = prefix + sid; Integer maxAge = null; JsonObject obj = sess.getObject("cookie"); if (obj != null) { maxAge = obj.getInteger("maxAge"); } String session = sess.encode(); int ttl; if (maxAge != null) { ttl = maxAge / 1000; } else { ttl = this.ttl; } JsonObject redis = new JsonObject(); redis.putString("command", "setex"); redis.putArray("args", new JsonArray().add(sid).add(ttl).add(session)); eventBus.send( redisAddress, redis, new Handler<Message<JsonObject>>() { @Override public void handle(Message<JsonObject> reply) { if ("ok".equals(reply.body().getString("status"))) { callback.handle(null); } else { callback.handle(reply.body().getString("message")); } } }); }