@Test public void testDefaultListenerMethod() throws Exception { final AtomicBoolean called = new AtomicBoolean(false); class Delegate { @SuppressWarnings("unused") public String handleMessage(String input) { called.set(true); return "processed" + input; } } this.adapter.setDelegate(new Delegate()); this.adapter.onMessage(new Message("foo".getBytes(), messageProperties), null); assertTrue(called.get()); }
@Test public void testExplicitListenerMethod() throws Exception { this.adapter.setDefaultListenerMethod("handle"); this.adapter.setDelegate(this.simpleService); this.adapter.onMessage(new Message("foo".getBytes(), this.messageProperties), null); assertEquals("handle", this.simpleService.called); }
@Test public void testJdkProxyListener() throws Exception { this.adapter.setDefaultListenerMethod("handle"); ProxyFactory factory = new ProxyFactory(this.simpleService); factory.setProxyTargetClass(false); this.adapter.setDelegate(factory.getProxy()); this.adapter.onMessage(new Message("foo".getBytes(), this.messageProperties), null); assertEquals("handle", this.simpleService.called); }
@Test public void testProxyListener() throws Exception { this.adapter.setDefaultListenerMethod("notDefinedOnInterface"); ProxyFactory factory = new ProxyFactory(this.simpleService); factory.setProxyTargetClass(true); this.adapter.setDelegate(factory.getProxy()); this.adapter.onMessage(new Message("foo".getBytes(), this.messageProperties), null); assertEquals("notDefinedOnInterface", this.simpleService.called); }
@Test public void testReplyRetry() throws Exception { this.adapter.setDefaultListenerMethod("handle"); this.adapter.setDelegate(this.simpleService); RetryPolicy retryPolicy = new SimpleRetryPolicy(2); RetryTemplate retryTemplate = new RetryTemplate();
@Test public void testMappedListenerMethod() throws Exception { Map<String, String> map = new HashMap<String, String>(); map.put("foo", "handle"); map.put("bar", "notDefinedOnInterface"); this.adapter.setDefaultListenerMethod("anotherHandle"); this.adapter.setQueueOrTagToMethodName(map); this.adapter.setDelegate(this.simpleService); this.messageProperties.setConsumerQueue("foo"); this.messageProperties.setConsumerTag("bar"); this.adapter.onMessage(new Message("foo".getBytes(), this.messageProperties), null); assertEquals("handle", this.simpleService.called); this.messageProperties.setConsumerQueue("junk"); this.adapter.onMessage(new Message("foo".getBytes(), this.messageProperties), null); assertEquals("notDefinedOnInterface", this.simpleService.called); this.messageProperties.setConsumerTag("junk"); this.adapter.onMessage(new Message("foo".getBytes(), this.messageProperties), null); assertEquals("anotherHandle", this.simpleService.called); }
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(template.getConnectionFactory()); MessageListenerAdapter messageListener = new MessageListenerAdapter(); messageListener.setDelegate(new Object()); container.setMessageListener(messageListener);