@Override @SuppressWarnings("unchecked") public T read() { if (itemType != null && itemType.isAssignableFrom(Message.class)) { return (T) amqpTemplate.receive(); } Object result = amqpTemplate.receiveAndConvert(); if (itemType != null && result != null) { Assert.state(itemType.isAssignableFrom(result.getClass()), "Received message payload of wrong type: expected [" + itemType + "]"); } return (T) result; }
@Test public void testNoItemType() { final AmqpTemplate amqpTemplate = mock(AmqpTemplate.class); when(amqpTemplate.receiveAndConvert()).thenReturn("foo"); final AmqpItemReader<String> amqpItemReader = new AmqpItemReader<>(amqpTemplate); assertEquals("foo", amqpItemReader.read()); }
@Test public void testTypeMismatch() { final AmqpTemplate amqpTemplate = mock(AmqpTemplate.class); when(amqpTemplate.receiveAndConvert()).thenReturn("foo"); final AmqpItemReader<Integer> amqpItemReader = new AmqpItemReader<>(amqpTemplate); amqpItemReader.setItemType(Integer.class); try { amqpItemReader.read(); fail("Expected IllegalStateException"); } catch (IllegalStateException e) { assertTrue(e.getMessage().contains("wrong type")); } }
@Test public void testNonMessageItemType() { final AmqpTemplate amqpTemplate = mock(AmqpTemplate.class); when(amqpTemplate.receiveAndConvert()).thenReturn("foo"); final AmqpItemReader<String> amqpItemReader = new AmqpItemReader<>(amqpTemplate); amqpItemReader.setItemType(String.class); assertEquals("foo", amqpItemReader.read()); }
@Test public void testNoItemType() { when(this.amqpTemplate.receiveAndConvert()).thenReturn("foo"); final AmqpItemReader<String> amqpItemReader = new AmqpItemReaderBuilder<String>() .amqpTemplate(this.amqpTemplate).build(); assertEquals("foo", amqpItemReader.read()); }
@Test public void testNonMessageItemType() { when(this.amqpTemplate.receiveAndConvert()).thenReturn("foo"); final AmqpItemReader<String> amqpItemReader = new AmqpItemReaderBuilder<String>() .amqpTemplate(this.amqpTemplate).itemType(String.class).build(); assertEquals("foo", amqpItemReader.read()); }
return getAmqpTemplate().receiveAndConvert(this.queue.getName()); return getAmqpTemplate().receiveAndConvert(this.queue.getName(), timeout);
public static void main(String[] args) { ApplicationContext context = new AnnotationConfigApplicationContext(HelloWorldConfiguration.class); AmqpTemplate amqpTemplate = context.getBean(AmqpTemplate.class); System.out.println("Received: " + amqpTemplate.receiveAndConvert()); }
@Override @SuppressWarnings("unchecked") public T read() { if (itemType != null && itemType.isAssignableFrom(Message.class)) { return (T) amqpTemplate.receive(); } Object result = amqpTemplate.receiveAndConvert(); if (itemType != null && result != null) { Assert.state(itemType.isAssignableFrom(result.getClass()), "Received message payload of wrong type: expected [" + itemType + "]"); } return (T) result; }
@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); }
return getAmqpTemplate().receiveAndConvert(this.queue.getName()); return getAmqpTemplate().receiveAndConvert(this.queue.getName(), timeout);
@Test public void testAmqpInboundGatewayFlow() throws Exception { 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); assertSame(this.amqpTemplate, TestUtils.getPropertyValue(this.amqpInboundGateway, "amqpTemplate")); }