Add alternate constructor to MessageListenerAdapter

As a convenience when configuring with Java, added another constructor
call that allows setting the delegate and the default listener method
in one line of code.

```java
	public MessageListenerAdapter(Object delegate, String defaultListenerMethod) {
		this(delegate);
		setDefaultListenerMethod(defaultListenerMethod);
	}
```

This supports configuring a POJO-based listener with a single step:

```java
	@Test
	public void testCustomMethodWithAlternateConstructor() throws Exception {
		MessageListenerAdapter adapter = new MessageListenerAdapter(target, "customMethod");
		adapter.afterPropertiesSet();

		adapter.onMessage(STRING_MSG, null);

		verify(target).customMethod(PAYLOAD);
	}
```

A pure Java configuration bean can now look like this:
```java
	 @Bean
	 MessageListenerAdapter listenerAdapter(Receiver receiver) {
		 return new new MessageListenerAdapter(receiver, "pojoMethod");
	 }
	 @Bean
	 Receiver receiver() {
		 return new Receiver();
	 }
```
No longer do we have to A) assign the adapter to a variable and B) call
setDefaultListenerMethod, slimming down pure POJO configuration with pure
Java.
This commit is contained in:
Greg Turnquist
2013-05-31 11:16:31 -04:00
parent 7c8dafce67
commit 49dc960ab2
2 changed files with 36 additions and 2 deletions

View File

@@ -27,17 +27,17 @@ import org.mockito.MockitoAnnotations;
import org.springframework.data.redis.connection.DefaultMessage;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* Unit test for MessageListenerAdapter.
*
* @author Costin Leau
* @author Greg Turnquist
*/
public class MessageListenerTest {
private static final RedisSerializer serializer = new StringRedisSerializer();
private static final StringRedisSerializer serializer = new StringRedisSerializer();
private static final String CHANNEL = "some::test:";
private static final byte[] RAW_CHANNEL = serializer.serialize(CHANNEL);
private static final String PAYLOAD = "do re mi";
@@ -107,6 +107,16 @@ public class MessageListenerTest {
verify(target).customMethod(PAYLOAD);
}
@Test
public void testCustomMethodWithAlternateConstructor() throws Exception {
MessageListenerAdapter adapter = new MessageListenerAdapter(target, "customMethod");
adapter.afterPropertiesSet();
adapter.onMessage(STRING_MSG, null);
verify(target).customMethod(PAYLOAD);
}
@Test
public void testCustomMethodWithChannel() throws Exception {
MessageListenerAdapter adapter = new MessageListenerAdapter(target);
@@ -118,6 +128,16 @@ public class MessageListenerTest {
verify(target).customMethodWithChannel(PAYLOAD, CHANNEL);
}
@Test
public void testCustomMethodWithChannelAndAlternateConstructor() throws Exception {
MessageListenerAdapter adapter = new MessageListenerAdapter(target, "customMethodWithChannel");
adapter.afterPropertiesSet();
adapter.onMessage(STRING_MSG, RAW_CHANNEL);
verify(target).customMethodWithChannel(PAYLOAD, CHANNEL);
}
/**
* @see DATAREDIS-92
*/