@Scheduled(fixedDelay = 1000, initialDelay = 500) public void send() { String message = "Hello World!"; this.template.convertAndSend(queue.getName(), message); System.out.println(" [x] Sent '" + message + "'"); }
@Scheduled(fixedDelay = 1000, initialDelay = 500) public void send() { StringBuilder builder = new StringBuilder("Hello"); if (dots.getAndIncrement() == 3) { dots.set(1); } for (int i = 0; i < dots.get(); i++) { builder.append('.'); } builder.append(count.incrementAndGet()); String message = builder.toString(); template.convertAndSend(queue.getName(), message); System.out.println(" [x] Sent '" + message + "'"); }
@Override protected String obtainQueueName(String channelName) { if (this.queue == null) { this.queue = new Queue(channelName); } return this.queue.getName(); }
@Override protected String getRoutingKey() { return this.queue != null ? this.queue.getName() : super.getRoutingKey(); }
@Override protected String getRoutingKey() { return this.queue != null ? this.queue.getName() : super.getRoutingKey(); }
@Override protected void doDeclares() { AmqpAdmin admin = getAdmin(); if (admin != null && admin.getQueueProperties(this.queue.getName()) == null) { admin.declareQueue(this.queue); } }
@Override protected String obtainQueueName(String channelName) { if (this.exchange == null) { String exchangeName = "si.fanout." + channelName; this.exchange = new FanoutExchange(exchangeName); } this.binding = BindingBuilder.bind(this.queue).to(this.exchange); return this.queue.getName(); }
@Override protected void doDeclares() { AmqpAdmin admin = getAdmin(); if (admin != null && this.queue != null && admin.getQueueProperties(this.queue.getName()) == null) { admin.declareQueue(this.queue); } }
@Override protected void doDeclares() { AmqpAdmin admin = getAdmin(); if (admin != null) { if (admin.getQueueProperties(this.queue.getName()) == null) { admin.declareQueue(this.queue); } if (this.exchange != null) { admin.declareExchange(this.exchange); } if (this.binding != null) { admin.declareBinding(this.binding); } } }
@Bean public RabbitTemplate template() { RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory()); rabbitTemplate.setRoutingKey(queue().getName()); return rabbitTemplate; }
return getAmqpTemplate().receiveAndConvert(this.queue.getName()); return getAmqpTemplate().receiveAndConvert(this.queue.getName(), timeout); org.springframework.amqp.core.Message message; if (timeout == null) { message = rabbitTemplate.receive(this.queue.getName()); message = rabbitTemplate.receive(this.queue.getName(), timeout);
@Bean public IntegrationFlow amqpAsyncOutboundFlow(AsyncRabbitTemplate asyncRabbitTemplate) { return f -> f .handle(Amqp.asyncOutboundGateway(asyncRabbitTemplate) .routingKeyFunction(m -> queue().getName()), e -> e.id("asyncOutboundGateway")); }
@Bean public Queue exQueue() { return new Queue("si.dsl.exception.test", true, false, false, new StringObjectMapBuilder() .put("x-dead-letter-exchange", "") .put("x-dead-letter-routing-key", exDLQ().getName()) .get()); }
@Bean public Queue exConvQueue() { return new Queue("si.dsl.conv.exception.test", true, false, false, new StringObjectMapBuilder() .put("x-dead-letter-exchange", "") .put("x-dead-letter-routing-key", exConvDLQ().getName()) .get()); }
@Test public void testAmqpInboundGatewayFlow() { assertNotNull(this.amqpInboundGatewayContainer); assertSame(this.amqpTemplate, TestUtils.getPropertyValue(this.amqpInboundGateway, "amqpTemplate")); Object result = this.amqpTemplate.convertSendAndReceive(this.amqpQueue.getName(), "world"); assertEquals("HELLO WORLD", result); this.amqpInboundGateway.stop(); //INTEXT-209 this.amqpInboundGateway.start(); this.amqpTemplate.convertAndSend(this.amqpQueue.getName(), "world"); ((RabbitTemplate) this.amqpTemplate).setReceiveTimeout(10000); result = this.amqpTemplate.receiveAndConvert("defaultReplyTo"); assertEquals("HELLO WORLD", result); }
@Test public void testGatewayPublisherConfirms() throws Exception { while (this.amqpTemplateConfirms.receive(this.queue.getName()) != null) { assertEquals(Boolean.TRUE, ack.getHeaders().get(AmqpHeaders.PUBLISH_CONFIRM)); org.springframework.amqp.core.Message received = this.amqpTemplateConfirms.receive(this.queue.getName()); assertEquals("\"hello\"", new String(received.getBody(), "UTF-8")); assertEquals("application/json", received.getMessageProperties().getContentType()); assertEquals(Boolean.TRUE, ack.getHeaders().get(AmqpHeaders.PUBLISH_CONFIRM)); while (this.amqpTemplateConfirms.receive(this.queue.getName()) != null) {
@Bean public IntegrationFlow amqpFlow(ConnectionFactory rabbitConnectionFactory, AmqpTemplate amqpTemplate) { return IntegrationFlows .from(Amqp.inboundGateway(rabbitConnectionFactory, amqpTemplate, queue()) .id("amqpInboundGateway") .configureContainer(c -> c .id("amqpInboundGatewayContainer") .recoveryInterval(5000) .concurrentConsumers(1)) .defaultReplyTo(defaultReplyTo().getName())) .transform("hello "::concat) .transform(String.class, String::toUpperCase) .get(); }
@Test public void adapterWithContentType() throws Exception { RabbitTemplate template = new RabbitTemplate(this.connectionFactory); template.setDefaultReceiveQueue(this.queue.getName()); while (template.receive() != null) { // drain } Message<?> message = MessageBuilder.withPayload("hello") .setHeader(AmqpHeaders.CONTENT_TYPE, "application/json") .build(); this.ctRequestChannel.send(message); org.springframework.amqp.core.Message m = receive(template); assertNotNull(m); assertEquals("\"hello\"", new String(m.getBody(), "UTF-8")); assertEquals("application/json", m.getMessageProperties().getContentType()); assertEquals("java.lang.String", m.getMessageProperties().getHeaders().get(JsonHeaders.TYPE_ID.replaceFirst(JsonHeaders.PREFIX, ""))); message = MessageBuilder.withPayload("hello") .build(); this.ctRequestChannel.send(message); m = receive(template); assertNotNull(m); assertEquals("hello", new String(m.getBody(), "UTF-8")); assertEquals("text/plain", m.getMessageProperties().getContentType()); while (template.receive() != null) { // drain } }
public IntegrationFlow amqpDMLCFlow(ConnectionFactory rabbitConnectionFactory, AmqpTemplate amqpTemplate) { return IntegrationFlows .from(Amqp.inboundGateway(new DirectMessageListenerContainer()) .id("amqpInboundGateway") .configureContainer(c -> c .recoveryInterval(5000) .consumersPerQueue(1)) .defaultReplyTo(defaultReplyTo().getName())) .transform("hello "::concat) .transform(String.class, String::toUpperCase) .get(); }
@Override public void addQueue(String vhost, Queue queue) { QueueInfo info = new QueueInfo(); info.setArguments(queue.getArguments()); info.setAutoDelete(queue.isAutoDelete()); info.setDurable(queue.isDurable()); info.setExclusive(queue.isExclusive()); this.rabbitClient.declareQueue(vhost, queue.getName(), info); }