@Override public void sendMessage( String message ) { try { messages.put( new QueueingConsumer.Delivery( mock( Envelope.class ), new AMQP.BasicProperties(), message.getBytes() ) ); } catch( InterruptedException e ) { throw new RuntimeException( "Unable to send message: " + message, e ); } }
public LoggingEvent call() throws Exception { String id = String.format("%s:%s", appenderId, System.currentTimeMillis()); String routingKey = String.format(queueNameFormatString, event.getLevel().toString(), event.getLoggerName()); AMQP.BasicProperties props = new AMQP.BasicProperties(); props.setCorrelationId(id); props.setType(event.getLevel().toString()); props.setTimestamp(Calendar.getInstance().getTime()); getChannel().basicPublish(exchange, routingKey, props, message.toString().getBytes()); return event; } }
@Override public void emit(final Event event) { requireNonNull(event, "Cannot emit a null event!"); final BasicProperties props = new BasicProperties().builder() .contentType("application/ld+json").contentEncoding("UTF-8").build(); service.serialize(event).ifPresent(message -> { try { channel.basicPublish(exchangeName, routingKey, mandatory, immediate, props, message.getBytes()); } catch (final IOException ex) { LOGGER.error("Error writing to broker: {}", ex.getMessage()); } }); }
RabbitTemplate template = new RabbitTemplate(cf); template.execute(c -> { c.basicPublish("", "test.shutdown", new BasicProperties(), "foo".getBytes()); RabbitUtils.setPhysicalCloseRequired(c, false); return null;
AMQP.BasicProperties.Builder builder = new AMQP.BasicProperties().builder(); if (message.getTimestamp() != null) {
container.setMessageListener((MessageListener) message -> messages.add(message)); container.start(); BasicProperties props = new BasicProperties(); byte[] payload = "baz".getBytes(); Envelope envelope = new Envelope(1L, false, "foo", "bar");
protected void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body) throws IOException { String correlationId = properties.getCorrelationId(); String replyTo = properties.getReplyTo(); if (correlationId != null && replyTo != null) { // decode the event T event = this.transcoder.decodeFromBytes(body); // handle the event R response = this.handler.handleDevliery(event); // ack the event this.channel.basicAck(envelope.getDeliveryTag(), false); // send the response if (response != null) { this.channel.basicPublish( "", replyTo, new BasicProperties("application/json", null, null, 1, null, correlationId, null, null, null, null, null, null, null, null), this.responseTranscoder.encodeAsBytes(response) ); } } else { logger.warn("Ignoring request without correlationId or replyTo"); } }
@Override public Future<R> publish(K key, long timeout, T event, Consumer<R> onSuccess, Consumer<Exception> onError) { try { PendingRequest future = new PendingRequest(event, onSuccess, onError); // store the pending request this.pendingRequests.put(future.getId(), future); // schedule the timeout this.timer.schedule(future, timeout); // publish the request this.channel.basicPublish( this.exchange.getName(), key == null ? "" : key.toString(), new BasicProperties("application/json", null, null, 1, null, future.getId(), this.replyQueue.getName(), null, null, null, null, null, null, null), this.transcoder.encodeAsBytes(event) ); return future; } catch (IOException e) { throw new QueueException("Failed to publish request", e); } }
@Test // AMQP-249 public void dontHangConsumerThread() throws Exception { ConnectionFactory mockConnectionFactory = mock(ConnectionFactory.class); Connection mockConnection = mock(Connection.class); Channel mockChannel = mock(Channel.class); given(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).willReturn(mockConnection); given(mockConnection.isOpen()).willReturn(true); given(mockConnection.createChannel()).willReturn(mockChannel); given(mockChannel.queueDeclare()).willReturn(new AMQImpl.Queue.DeclareOk("foo", 0, 0)); final AtomicReference<Consumer> consumer = new AtomicReference<Consumer>(); willAnswer(invocation -> { consumer.set(invocation.getArgument(6)); return null; }).given(mockChannel).basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), isNull(), any(Consumer.class)); SingleConnectionFactory connectionFactory = new SingleConnectionFactory(mockConnectionFactory); connectionFactory.setExecutor(mock(ExecutorService.class)); RabbitTemplate template = new RabbitTemplate(connectionFactory); template.setReplyTimeout(1); Message input = new Message("Hello, world!".getBytes(), new MessageProperties()); template.doSendAndReceiveWithTemporary("foo", "bar", input, null); Envelope envelope = new Envelope(1, false, "foo", "bar"); // used to hang here because of the SynchronousQueue and doSendAndReceive() already exited consumer.get().handleDelivery("foo", envelope, new AMQP.BasicProperties(), new byte[0]); }
/** * Method is called by ExecutorService and publishes message on RabbitMQ * * @return loggingEvent */ @Override public LoggingEvent call() throws Exception { if(zafiraConnected) { String payload = layout.format(loggingEvent); AMQP.BasicProperties.Builder b = new AMQP.BasicProperties().builder(); b.appId(identifier) .type(loggingEvent.getLevel().toString()) .correlationId(String.valueOf(correlationId)) .contentType("text/json"); createChannel().basicPublish(exchange, routingKey, b.build(), payload.getBytes()); } return loggingEvent; } }
/** * Method is called by ExecutorService and publishes message on RabbitMQ * * @return loggingEvent */ @Override public LoggingEvent call() throws Exception { if(zafiraConnected) { String payload = layout.format(loggingEvent); AMQP.BasicProperties.Builder b = new AMQP.BasicProperties().builder(); b.appId(identifier) .type(loggingEvent.getLevel().toString()) .correlationId(String.valueOf(correlationId)) .contentType("text/json"); createChannel().basicPublish(exchange, routingKey, b.build(), payload.getBytes()); } return loggingEvent; } }
/** * Build and populate the AMQP.BasicProperties using the RabbitMQMessage * * @param message the RabbitMQMessage to be used to get the properties * @return AMQP.BasicProperties object */ private AMQP.BasicProperties.Builder buildBasicProperties(RabbitMQMessage message) { AMQP.BasicProperties.Builder builder = new AMQP.BasicProperties().builder(); builder.messageId(message.getMessageId()); builder.contentType(message.getContentType()); builder.replyTo(message.getReplyTo()); builder.correlationId(message.getCorrelationId()); builder.contentEncoding(message.getContentEncoding()); Map<String, Object> headers = message.getHeaders(); if (message.getSoapAction() != null) { headers.put(RabbitMQConstants.SOAP_ACTION, message.getSoapAction()); } builder.headers(headers); return builder; }
public static AMQP.BasicProperties fromJson( JsonObject json ) { if ( json == null ) { return new AMQP.BasicProperties(); } return new AMQP.BasicProperties.Builder() .contentType(json.getString("contentType")) .contentEncoding(json.getString("contentEncoding")) .headers(asMap(json.getJsonObject("headers"))) .deliveryMode(json.getInteger("deliveryMode")) .priority(json.getInteger("priority")) .correlationId(json.getString("correlationId")) .replyTo(json.getString("replyTo")) .expiration(json.getString("expiration")) .messageId(json.getString("messageId")) .timestamp(parseDate(json.getString("timestamp"))) .type(json.getString("type")) .userId(json.getString("userId")) .appId(json.getString("appId")) .clusterId(json.getString("clusterId")).build(); }
public MessageWrapper() { this.properties = new AMQP.BasicProperties().builder(); }
channel.basicPublish("celery", "celery", null, new AMQP.BasicProperties().builder().contentType("application/json").build(), message.getBytes("UTF-8"));
private AMQP.BasicProperties build() { return new AMQP.BasicProperties( contentType, contentEncoding, headers, deliveryMode, priority, correlationId, replyTo, expiration, messageId, timestamp, type, userId, appId, clusterId ); }
@Override public void emit(final Event event) { requireNonNull(event, "Cannot emit a null event!"); final BasicProperties props = new BasicProperties().builder() .contentType("application/ld+json").contentEncoding("UTF-8").build(); service.serialize(event).ifPresent(message -> { try { channel.basicPublish(exchangeName, routingKey, mandatory, immediate, props, message.getBytes()); } catch (final IOException ex) { LOGGER.error("Error writing to broker: {}", ex.getMessage()); } }); }
private AMQP.BasicProperties build() { return new AMQP.BasicProperties( contentType, contentEncoding, headers, deliveryMode, priority, correlationId, replyTo, expiration, messageId, timestamp, type, userId, appId, clusterId ); }
private AMQP.BasicProperties createHeader(String msgType) { AMQP.BasicProperties.Builder header = new AMQP.BasicProperties().builder(); Map<String, Object> props = new HashMap<String, Object>(); props.put("type", msgType); header.headers(props); return header.build(); }
new AMQP.BasicProperties("application/json", "ASCII", null, null, null, null, null, null, null, null, null, "guest", null, null), messageBody.getBytes("ASCII")); } catch (IOException ioe) {