List of usage examples for com.rabbitmq.client Delivery getEnvelope
public Envelope getEnvelope()
From source file:com.hopped.running.rabbitmq.rpc.ARPCServer.java
License:Open Source License
/** * // www.j a v a2 s. co m */ public void consume() { checkConsumer(); while (true) { try { Delivery delivery = consumer.nextDelivery(); BasicProperties props = delivery.getProperties(); BasicProperties replyProps = new BasicProperties.Builder().correlationId(props.getCorrelationId()) .build(); channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); byte[] payload = processRequest(delivery); channel.basicPublish("", props.getReplyTo(), replyProps, payload); } catch (ShutdownSignalException | ConsumerCancelledException | InterruptedException | IOException e) { logger.error(e.getMessage()); closeConnection(); } } }
From source file:com.surgeplay.visage.master.VisageMaster.java
License:Open Source License
@Override public void run() { try {/*from w w w .j a v a2 s. c o m*/ Log.setLog(new LogShim(Visage.log)); long total = Runtime.getRuntime().totalMemory(); long max = Runtime.getRuntime().maxMemory(); if (Visage.debug) Visage.log.finer("Current heap size: " + humanReadableByteCount(total, false)); if (Visage.debug) Visage.log.finer("Max heap size: " + humanReadableByteCount(max, false)); if (total < max) { Visage.log.warning( "You have set your minimum heap size (Xms) lower than the maximum heap size (Xmx) - this can cause GC thrashing. It is strongly recommended to set them both to the same value."); } if (max < (1000 * 1000 * 1000)) { Visage.log.warning( "The heap size (Xmx) is less than one gigabyte; it is recommended to run Visage with a gigabyte or more. Use -Xms1G and -Xmx1G to do this."); } Visage.log.info("Setting up Jetty"); Server server = new Server( new InetSocketAddress(config.getString("http.bind"), config.getInt("http.port"))); List<String> expose = config.getStringList("expose"); String poweredBy; if (expose.contains("server")) { if (expose.contains("version")) { poweredBy = "Visage v" + Visage.VERSION; } else { poweredBy = "Visage"; } } else { poweredBy = null; } ResourceHandler resource = new ResourceHandler(); resource.setResourceBase(config.getString("http.static")); resource.setDirectoriesListed(false); resource.setWelcomeFiles(new String[] { "index.html" }); resource.setHandler(new VisageHandler(this)); if (!"/dev/null".equals(config.getString("log"))) { new File(config.getString("log")).getParentFile().mkdirs(); server.setRequestLog(new AsyncNCSARequestLog(config.getString("log"))); } GzipHandler gzip = new GzipHandler(); gzip.setHandler(new HeaderHandler("X-Powered-By", poweredBy, resource)); server.setHandler(gzip); String redisHost = config.getString("redis.host"); int redisPort = config.getInt("redis.port"); Visage.log.info("Connecting to Redis at " + redisHost + ":" + redisPort); resolverNum = config.getInt("redis.resolver-db"); skinNum = config.getInt("redis.skin-db"); JedisPoolConfig jpc = new JedisPoolConfig(); jpc.setMaxIdle(config.getInt("redis.max-idle-connections")); jpc.setMaxTotal(config.getInt("redis.max-total-connections")); jpc.setMinIdle(config.getInt("redis.min-idle-connections")); if (config.hasPath("redis.password")) { password = config.getString("redis.password"); } pool = new JedisPool(jpc, redisHost, redisPort); Visage.log.info("Connecting to RabbitMQ at " + config.getString("rabbitmq.host") + ":" + config.getInt("rabbitmq.port")); ConnectionFactory factory = new ConnectionFactory(); factory.setHost(config.getString("rabbitmq.host")); factory.setPort(config.getInt("rabbitmq.port")); factory.setRequestedHeartbeat(10); if (config.hasPath("rabbitmq.user")) { factory.setUsername(config.getString("rabbitmq.user")); factory.setPassword(config.getString("rabbitmq.password")); } String queue = config.getString("rabbitmq.queue"); Closer closer = Closer.create(); steve = ByteStreams.toByteArray(closer.register(ClassLoader.getSystemResourceAsStream("steve.png"))); alex = ByteStreams.toByteArray(closer.register(ClassLoader.getSystemResourceAsStream("alex.png"))); closer.close(); conn = factory.newConnection(); channel = conn.createChannel(); if (Visage.debug) Visage.log.finer("Setting up queue '" + queue + "'"); channel.queueDeclare(queue, false, false, true, null); channel.basicQos(1); if (Visage.debug) Visage.log.finer("Setting up reply queue"); replyQueue = channel.queueDeclare().getQueue(); consumer = new QueueingConsumer(channel); channel.basicConsume(replyQueue, consumer); if (config.getBoolean("slave.enable")) { Visage.log.info("Starting fallback slave"); fallback = new VisageSlave( config.getConfig("slave").withValue("rabbitmq", config.getValue("rabbitmq"))); fallback.start(); } Visage.log.info("Starting Jetty"); server.start(); Visage.log.info("Listening for finished jobs"); try { while (run) { Delivery delivery = consumer.nextDelivery(); if (Visage.trace) Visage.log.finest("Got delivery"); try { String corrId = delivery.getProperties().getCorrelationId(); if (queuedJobs.containsKey(corrId)) { if (Visage.trace) Visage.log.finest("Valid"); responses.put(corrId, delivery.getBody()); Runnable run = queuedJobs.get(corrId); queuedJobs.remove(corrId); if (Visage.trace) Visage.log.finest("Removed from queue"); run.run(); if (Visage.trace) Visage.log.finest("Ran runnable"); channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); if (Visage.trace) Visage.log.finest("Ack'd"); } else { Visage.log.warning("Unknown correlation ID?"); channel.basicNack(delivery.getEnvelope().getDeliveryTag(), false, false); } } catch (Exception e) { Visage.log.log(Level.WARNING, "An unexpected error occured while attempting to process a response.", e); } } } catch (InterruptedException e) { } catch (Exception e) { Visage.log.log(Level.SEVERE, "An unexpected error occured in the master run loop.", e); System.exit(2); } try { Visage.log.info("Shutting down master"); server.stop(); pool.destroy(); conn.close(5000); } catch (Exception e) { Visage.log.log(Level.SEVERE, "A fatal error has occurred while shutting down the master.", e); } } catch (Exception e) { Visage.log.log(Level.SEVERE, "An unexpected error occured while initializing the master.", e); System.exit(1); } }
From source file:com.surgeplay.visage.slave.RenderThread.java
License:Open Source License
@Override public void run() { try {//w w w.j ava 2s . c o m Visage.log.info("Waiting for jobs"); try { while (run) { if (!toProcess.isEmpty()) { Delivery delivery = toProcess.pop(); try { processDelivery(delivery); } catch (Exception e) { Visage.log.log(Level.SEVERE, "An unexpected error occurred while rendering", e); BasicProperties props = delivery.getProperties(); BasicProperties replyProps = new BasicProperties.Builder() .correlationId(props.getCorrelationId()).build(); ByteArrayOutputStream ex = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(ex); oos.writeObject(e); oos.flush(); parent.channel.basicPublish("", props.getReplyTo(), replyProps, buildResponse(1, ex.toByteArray())); parent.channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); } } else { synchronized (toProcess) { toProcess.wait(); } } } for (Renderer r : renderers) { if (r != null) { r.destroy(); } } } catch (Exception e) { Visage.log.log(Level.SEVERE, "A fatal error has occurred in the render thread run loop.", e); } } catch (Exception e) { Visage.log.log(Level.SEVERE, "A fatal error has occurred while setting up a render thread.", e); } }
From source file:com.surgeplay.visage.slave.RenderThread.java
License:Open Source License
private void processDelivery(Delivery delivery) throws Exception { BasicProperties props = delivery.getProperties(); BasicProperties replyProps = new BasicProperties.Builder().correlationId(props.getCorrelationId()).build(); DataInputStream data = new DataInputStream( new InflaterInputStream(new ByteArrayInputStream(delivery.getBody()))); RenderMode mode = RenderMode.values()[data.readUnsignedByte()]; int width = data.readUnsignedShort(); int height = data.readUnsignedShort(); int supersampling = data.readUnsignedByte(); GameProfile profile = Profiles.readGameProfile(data); Map<String, String[]> params = Maps.newHashMap(); int len = data.readUnsignedShort(); for (int i = 0; i < len; i++) { String key = data.readUTF(); String[] val = new String[data.readUnsignedByte()]; for (int v = 0; v < val.length; v++) { val[v] = data.readUTF(); }/* ww w. j ava 2 s. c o m*/ params.put(key, val); } byte[] skinData = new byte[data.readInt()]; data.readFully(skinData); BufferedImage skinRaw = new PngImage().read(new ByteArrayInputStream(skinData), false); BufferedImage skin = Images.toARGB(skinRaw); Visage.log.info("Received a job to render a " + width + "x" + height + " " + mode.name().toLowerCase() + " (" + supersampling + "x supersampling) for " + (profile == null ? "null" : profile.getName())); byte[] pngBys = draw(mode, width, height, supersampling, profile, skin, params); if (Visage.trace) Visage.log.finest("Got png bytes"); parent.channel.basicPublish("", props.getReplyTo(), replyProps, buildResponse(0, pngBys)); if (Visage.trace) Visage.log.finest("Published response"); parent.channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); if (Visage.trace) Visage.log.finest("Ack'd message"); }
From source file:it.polimi.hegira.adapters.datastore.Datastore.java
License:Apache License
@Override protected AbstractDatabase fromMyModel(Metamodel mm) { // TWC//w w w . j a v a 2 s .c om //log.debug(Thread.currentThread().getName()+" Hi I'm the GAE consumer!"); List<Entity> batch = new ArrayList<Entity>(); TDeserializer deserializer = new TDeserializer(new TBinaryProtocol.Factory()); long k = 0; int thread_id = 0; if (TWTs_NO != 0) thread_id = (int) (Thread.currentThread().getId() % TWTs_NO); while (true) { log.debug(Thread.currentThread().getName() + " Extracting from the taskQueue" + thread_id + " TWTs_NO: " + TWTs_NO); try { Delivery delivery = taskQueues.get(thread_id).getConsumer().nextDelivery(2000); if (delivery != null) { Metamodel myModel = new Metamodel(); deserializer.deserialize(myModel, delivery.getBody()); DatastoreTransformer dt = new DatastoreTransformer(connectionList.get(thread_id).ds); DatastoreModel fromMyModel = dt.fromMyModel(myModel); batch.add(fromMyModel.getEntity()); batch.add(fromMyModel.getFictitiousEntity()); taskQueues.get(thread_id).sendAck(delivery.getEnvelope().getDeliveryTag()); k++; if (k % 100 == 0) { putBatch(batch); log.debug(Thread.currentThread().getName() + " ===>100 entities. putting normal batch"); batch = new ArrayList<Entity>(); } else { if (k > 0) { //log.debug(Thread.currentThread().getName()+" ===>Nothing in the queue for me!"); putBatch(batch); log.debug(Thread.currentThread().getName() + " ===>less than 100 entities. putting short batch"); batch = new ArrayList<Entity>(); k = 0; } } } } catch (ShutdownSignalException | ConsumerCancelledException | InterruptedException e) { log.error("Error consuming from the queue " + TaskQueue.getDefaultTaskQueueName(), e); } catch (TException e) { log.error("Errore deserializing", e); } catch (QueueException e) { log.error("Couldn't send the ack to the queue " + TaskQueue.getDefaultTaskQueueName(), e); } } }
From source file:it.polimi.hegira.queue.TaskQueue.java
License:Apache License
/** * Acknowledge a message given its delivery (returned from the QueuingConsumer object). * @param delivery//from w w w.j a va2s . c o m * @throws QueueException */ public void sendAck(Delivery delivery) throws QueueException { try { channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); } catch (IOException e) { throw new QueueException(e.getMessage(), e.getCause()); } }
From source file:it.polimi.hegira.queue.TaskQueue.java
License:Apache License
/** * In case a message cannot be processed properly a negative acknowledgment must be sent. * @param delivery The message that was not processed. * @throws QueueException// w ww. jav a2 s . co m */ public void sendNack(Delivery delivery) throws QueueException { try { /** * void basicNack(long deliveryTag, * boolean multiple, * boolean requeue) */ channel.basicNack(delivery.getEnvelope().getDeliveryTag(), false, true); /** * void basicReject(long deliveryTag, * boolean requeue) */ channel.basicReject(delivery.getEnvelope().getDeliveryTag(), true); } catch (IOException e) { throw new QueueException(e.getMessage(), e.getCause()); } }
From source file:it.polimi.hegira.queue.TestQueue.java
License:Apache License
/** * Test method for {@link it.polimi.hegira.queue.Queue#publish(java.lang.String, byte[])}. * @throws QueueException /*from ww w .j a v a 2 s .c om*/ */ public void testPublish() { String logs = Thread.currentThread().getContextClassLoader().getResource(Constants.LOGS_PATH).getFile(); PropertyConfigurator.configure(logs); String RK = "toApiServer", MSG = "test message"; try { Queue queue = new Queue(); queue.publish(RK, MSG.getBytes()); QueueingConsumer consumer = queue.getConsumer(); Delivery delivery = consumer.nextDelivery(); String message = new String(delivery.getBody()); String routingKey = delivery.getEnvelope().getRoutingKey(); assertEquals(message, MSG); assertEquals(routingKey, RK); } catch (ShutdownSignalException | ConsumerCancelledException | InterruptedException e) { e.printStackTrace(); fail("Error retrieving the message from the queue"); } catch (QueueException e) { e.printStackTrace(); fail("Queue error"); } }
From source file:normalizerbankfour.NormalizerBankFour.java
/** * @param args the command line arguments *///from w w w. ja va 2 s .c om public static void main(String[] args) { ConnectionCreator creator = ConnectionCreator.getInstance(); try { channelIn = creator.createChannel(); channelIn.queueDeclare(IN_QUEUE, false, false, false, null); channelOut = creator.createChannel(); channelOut.queueDeclare(OUT_QUEUE, false, false, false, null); consumer = new QueueingConsumer(channelIn); channelIn.basicConsume(IN_QUEUE, consumer); } catch (IOException ex) { Logger.getLogger(NormalizerBankFour.class.getName()).log(Level.SEVERE, null, ex); } while (true) { try { System.out.println("Normalizer for BankFour is running"); Delivery delivery = consumer.nextDelivery(); channelIn.basicAck(delivery.getEnvelope().getDeliveryTag(), false); System.out.println("Got message: " + new String(delivery.getBody())); String message = normalizeMessage(new String(delivery.getBody())); BasicProperties probs = new BasicProperties().builder() .correlationId(delivery.getProperties().getCorrelationId()).build(); channelOut.basicPublish("", OUT_QUEUE, probs, message.getBytes()); } catch (InterruptedException ex) { Logger.getLogger(NormalizerBankFour.class.getName()).log(Level.SEVERE, null, ex); } catch (ShutdownSignalException ex) { Logger.getLogger(NormalizerBankFour.class.getName()).log(Level.SEVERE, null, ex); } catch (ConsumerCancelledException ex) { Logger.getLogger(NormalizerBankFour.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(NormalizerBankFour.class.getName()).log(Level.SEVERE, null, ex); } } }
From source file:org.apache.druid.firehose.rabbitmq.RabbitMQFirehoseFactory.java
License:Apache License
@Override public Firehose connect(final InputRowParser<ByteBuffer> firehoseParser, File temporaryDirectory) throws IOException { ConnectionOptions lyraOptions = new ConnectionOptions(this.connectionFactory); Config lyraConfig = new Config().withRecoveryPolicy(new RetryPolicy().withMaxRetries(config.getMaxRetries()) .withRetryInterval(Duration.seconds(config.getRetryIntervalSeconds())) .withMaxDuration(Duration.seconds(config.getMaxDurationSeconds()))); String queue = config.getQueue(); String exchange = config.getExchange(); String routingKey = config.getRoutingKey(); boolean durable = config.isDurable(); boolean exclusive = config.isExclusive(); boolean autoDelete = config.isAutoDelete(); final Connection connection = Connections.create(lyraOptions, lyraConfig); connection.addShutdownListener(new ShutdownListener() { @Override/*from w w w . ja va 2 s . c o m*/ public void shutdownCompleted(ShutdownSignalException cause) { log.warn(cause, "Connection closed!"); } }); final Channel channel = connection.createChannel(); channel.queueDeclare(queue, durable, exclusive, autoDelete, null); channel.queueBind(queue, exchange, routingKey); channel.addShutdownListener(new ShutdownListener() { @Override public void shutdownCompleted(ShutdownSignalException cause) { log.warn(cause, "Channel closed!"); } }); // We create a QueueingConsumer that will not auto-acknowledge messages since that // happens on commit(). final QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(queue, false, consumer); return new Firehose() { /** * Storing the latest row as a member variable should be safe since this will only be run * by a single thread. */ private InputRow nextRow; /** * Store the latest delivery tag to be able to commit (acknowledge) the message delivery up to * and including this tag. See commit() for more detail. */ private long lastDeliveryTag; private Iterator<InputRow> nextIterator = Collections.emptyIterator(); @Override public boolean hasMore() { nextRow = null; try { if (nextIterator.hasNext()) { nextRow = nextIterator.next(); return true; } // Wait for the next delivery. This will block until something is available. final Delivery delivery = consumer.nextDelivery(); if (delivery != null) { lastDeliveryTag = delivery.getEnvelope().getDeliveryTag(); nextIterator = firehoseParser.parseBatch(ByteBuffer.wrap(delivery.getBody())).iterator(); if (nextIterator.hasNext()) { nextRow = nextIterator.next(); // If delivery is non-null, we report that there is something more to process. return true; } } } catch (InterruptedException e) { // A little unclear on how we should handle this. // At any rate, we're in an unknown state now so let's log something and return false. log.wtf(e, "Got interrupted while waiting for next delivery. Doubt this should ever happen."); } // This means that delivery is null or we caught the exception above so we report that we have // nothing more to process. return false; } @Nullable @Override public InputRow nextRow() { if (nextRow == null) { //Just making sure. log.wtf("I have nothing in delivery. Method hasMore() should have returned false."); return null; } return nextRow; } @Override public Runnable commit() { // This method will be called from the same thread that calls the other methods of // this Firehose. However, the returned Runnable will be called by a different thread. // // It should be (thread) safe to copy the lastDeliveryTag like we do below and then // acknowledge values up to and including that value. return new Runnable() { // Store (copy) the last delivery tag to "become" thread safe. final long deliveryTag = lastDeliveryTag; @Override public void run() { try { log.info("Acknowledging delivery of messages up to tag: " + deliveryTag); // Acknowledge all messages up to and including the stored delivery tag. channel.basicAck(deliveryTag, true); } catch (IOException e) { log.error(e, "Unable to acknowledge message reception to message queue."); } } }; } @Override public void close() throws IOException { log.info("Closing connection to RabbitMQ"); channel.close(); connection.close(); } }; }