Polishing

This commit is contained in:
Juergen Hoeller
2014-08-22 22:55:57 +02:00
parent 28a966f544
commit b93dd95475
9 changed files with 108 additions and 113 deletions

View File

@@ -26,14 +26,14 @@ package org.springframework.messaging;
*/
public interface Message<T> {
/**
* Return message headers for the message (never {@code null}).
*/
MessageHeaders getHeaders();
/**
* Return the message payload.
*/
T getPayload();
/**
* Return message headers for the message (never {@code null} but may be empty).
*/
MessageHeaders getHeaders();
}

View File

@@ -59,7 +59,7 @@ import org.springframework.util.IdGenerator;
* </pre>
*
* A third option is to use {@link org.springframework.messaging.support.MessageHeaderAccessor}
* or one of its sub-classes to create specific categories of headers.
* or one of its subclasses to create specific categories of headers.
*
* @author Arjen Poutsma
* @author Mark Fisher
@@ -177,6 +177,7 @@ public class MessageHeaders implements Map<String, Object>, Serializable {
return (T) value;
}
@Override
public boolean equals(Object other) {
return (this == other ||
@@ -232,28 +233,32 @@ public class MessageHeaders implements Map<String, Object>, Serializable {
// Unsupported Map operations
/**
* Since MessageHeaders are immutable, the call to this method will result in {@link UnsupportedOperationException}.
* Since MessageHeaders are immutable, the call to this method
* will result in {@link UnsupportedOperationException}.
*/
public Object put(String key, Object value) {
throw new UnsupportedOperationException("MessageHeaders is immutable");
}
/**
* Since MessageHeaders are immutable, the call to this method will result in {@link UnsupportedOperationException}.
* Since MessageHeaders are immutable, the call to this method
* will result in {@link UnsupportedOperationException}.
*/
public void putAll(Map<? extends String, ? extends Object> t) {
public void putAll(Map<? extends String, ? extends Object> map) {
throw new UnsupportedOperationException("MessageHeaders is immutable");
}
/**
* Since MessageHeaders are immutable, the call to this method will result in {@link UnsupportedOperationException}.
* Since MessageHeaders are immutable, the call to this method
* will result in {@link UnsupportedOperationException}.
*/
public Object remove(Object key) {
throw new UnsupportedOperationException("MessageHeaders is immutable");
}
/**
* Since MessageHeaders are immutable, the call to this method will result in {@link UnsupportedOperationException}.
* Since MessageHeaders are immutable, the call to this method
* will result in {@link UnsupportedOperationException}.
*/
public void clear() {
throw new UnsupportedOperationException("MessageHeaders is immutable");

View File

@@ -16,17 +16,16 @@
package org.springframework.messaging.support;
import org.springframework.messaging.MessageHeaders;
import java.util.Map;
import org.springframework.messaging.MessageHeaders;
/**
* A {@link GenericMessage} with a {@link Throwable} payload.
*
* @author Mark Fisher
* @author Oleg Zhurakousky
* @since 4.0
*
* @see MessageBuilder
*/
public class ErrorMessage extends GenericMessage<Throwable> {
@@ -36,8 +35,7 @@ public class ErrorMessage extends GenericMessage<Throwable> {
/**
* Create a new message with the given payload.
*
* @param payload the message payload, never {@code null}
* @param payload the message payload (never {@code null})
*/
public ErrorMessage(Throwable payload) {
super(payload);
@@ -46,8 +44,7 @@ public class ErrorMessage extends GenericMessage<Throwable> {
/**
* Create a new message with the given payload and headers.
* The content of the given header map is copied.
*
* @param payload the message payload, never {@code null}
* @param payload the message payload (never {@code null})
* @param headers message headers to use for initialization
*/
public ErrorMessage(Throwable payload, Map<String, Object> headers) {
@@ -56,11 +53,9 @@ public class ErrorMessage extends GenericMessage<Throwable> {
/**
* A constructor with the {@link MessageHeaders} instance to use.
*
* <p><strong>Note:</strong> the given {@code MessageHeaders} instance is used
* directly in the new message, i.e. it is not copied.
*
* @param payload the message payload, never {@code null}
* <p><strong>Note:</strong> the given {@code MessageHeaders} instance
* is used directly in the new message, i.e. it is not copied.
* @param payload the message payload (never {@code null})
* @param headers message headers
*/
public ErrorMessage(Throwable payload, MessageHeaders headers) {

View File

@@ -42,18 +42,18 @@ public class ExecutorSubscribableChannel extends AbstractSubscribableChannel {
/**
* Create a new {@link ExecutorSubscribableChannel} instance where messages will be sent
* in the callers thread.
* Create a new {@link ExecutorSubscribableChannel} instance
* where messages will be sent in the callers thread.
*/
public ExecutorSubscribableChannel() {
this(null);
}
/**
* Create a new {@link ExecutorSubscribableChannel} instance where messages will be sent
* via the specified executor.
* @param executor the executor used to send the message or {@code null} to execute in
* the callers thread.
* Create a new {@link ExecutorSubscribableChannel} instance
* where messages will be sent via the specified executor.
* @param executor the executor used to send the message,
* or {@code null} to execute in the callers thread.
*/
public ExecutorSubscribableChannel(Executor executor) {
this.executor = executor;
@@ -100,6 +100,45 @@ public class ExecutorSubscribableChannel extends AbstractSubscribableChannel {
}
/**
* Helps with the invocation of configured executor channel interceptors.
*/
private class ExecutorChannelInterceptorChain {
private int interceptorIndex = -1;
public Message<?> applyBeforeHandle(Message<?> message, MessageChannel channel, MessageHandler handler) {
for (ExecutorChannelInterceptor interceptor : executorInterceptors) {
message = interceptor.beforeHandle(message, channel, handler);
if (message == null) {
String name = interceptor.getClass().getSimpleName();
if (logger.isDebugEnabled()) {
logger.debug(name + " returned null from beforeHandle, i.e. precluding the send.");
}
triggerAfterMessageHandled(message, channel, handler, null);
return null;
}
this.interceptorIndex++;
}
return message;
}
public void triggerAfterMessageHandled(Message<?> message, MessageChannel channel,
MessageHandler handler, Exception ex) {
for (int i = this.interceptorIndex; i >= 0; i--) {
ExecutorChannelInterceptor interceptor = executorInterceptors.get(i);
try {
interceptor.afterMessageHandled(message, channel, handler, ex);
}
catch (Throwable ex2) {
logger.error("Exception from afterMessageHandled in " + interceptor, ex2);
}
}
}
}
/**
* Helps with the invocation of the target MessageHandler and interceptors.
*/
@@ -113,7 +152,6 @@ public class ExecutorSubscribableChannel extends AbstractSubscribableChannel {
private final ExecutorChannelInterceptorChain chain;
public SendTask(Message<?> message, MessageChannel channel, MessageHandler handler,
ExecutorChannelInterceptorChain chain) {
@@ -151,43 +189,4 @@ public class ExecutorSubscribableChannel extends AbstractSubscribableChannel {
}
}
/**
* Helps with the invocation of configured executor channel interceptors.
*/
private class ExecutorChannelInterceptorChain {
private int interceptorIndex = -1;
public Message<?> applyBeforeHandle(Message<?> message, MessageChannel channel, MessageHandler handler) {
for (ExecutorChannelInterceptor interceptor : executorInterceptors) {
message = interceptor.beforeHandle(message, channel, handler);
if (message == null) {
String name = interceptor.getClass().getSimpleName();
if (logger.isDebugEnabled()) {
logger.debug(name + " returned null from beforeHandle, i.e. precluding the send.");
}
triggerAfterMessageHandled(message, channel, handler, null);
return null;
}
this.interceptorIndex++;
}
return message;
}
public void triggerAfterMessageHandled(Message<?> message, MessageChannel channel,
MessageHandler handler, Exception ex) {
for (int i = this.interceptorIndex; i >= 0; i--) {
ExecutorChannelInterceptor interceptor = executorInterceptors.get(i);
try {
interceptor.afterMessageHandled(message, channel, handler, ex);
}
catch (Throwable ex2) {
logger.error("Exception from afterMessageHandled in " + interceptor, ex2);
}
}
}
}
}

View File

@@ -30,7 +30,6 @@ import org.springframework.util.ObjectUtils;
*
* @author Mark Fisher
* @since 4.0
*
* @see MessageBuilder
*/
public class GenericMessage<T> implements Message<T>, Serializable {
@@ -45,8 +44,7 @@ public class GenericMessage<T> implements Message<T>, Serializable {
/**
* Create a new message with the given payload.
*
* @param payload the message payload, never {@code null}
* @param payload the message payload (never {@code null})
*/
public GenericMessage(T payload) {
this(payload, new MessageHeaders(null));
@@ -55,8 +53,7 @@ public class GenericMessage<T> implements Message<T>, Serializable {
/**
* Create a new message with the given payload and headers.
* The content of the given header map is copied.
*
* @param payload the message payload, never {@code null}
* @param payload the message payload (never {@code null})
* @param headers message headers to use for initialization
*/
public GenericMessage(T payload, Map<String, Object> headers) {
@@ -65,44 +62,27 @@ public class GenericMessage<T> implements Message<T>, Serializable {
/**
* A constructor with the {@link MessageHeaders} instance to use.
*
* <p><strong>Note:</strong> the given {@code MessageHeaders} instance is used
* directly in the new message, i.e. it is not copied.
*
* @param payload the message payload, never {@code null}
* @param payload the message payload (never {@code null})
* @param headers message headers
*/
public GenericMessage(T payload, MessageHeaders headers) {
Assert.notNull(payload, "'payload must not be null");
Assert.notNull(headers, "'headers' must not be null");
Assert.notNull(payload, "payload must not be null");
this.headers = headers;
this.payload = payload;
this.headers = headers;
}
public MessageHeaders getHeaders() {
return this.headers;
}
public T getPayload() {
return this.payload;
}
public String toString() {
StringBuilder sb = new StringBuilder();
if (this.payload instanceof byte[]) {
sb.append("[Payload byte[").append(((byte[]) this.payload).length).append("]]");
}
else {
sb.append("[Payload=").append(this.payload).append("]");
}
sb.append("[Headers=").append(this.headers).append("]");
return sb.toString();
public MessageHeaders getHeaders() {
return this.headers;
}
public int hashCode() {
return this.headers.hashCode() * 23 + ObjectUtils.nullSafeHashCode(this.payload);
}
public boolean equals(Object obj) {
if (this == obj) {
@@ -116,4 +96,20 @@ public class GenericMessage<T> implements Message<T>, Serializable {
return false;
}
public int hashCode() {
return this.headers.hashCode() * 23 + ObjectUtils.nullSafeHashCode(this.payload);
}
public String toString() {
StringBuilder sb = new StringBuilder(getClass().getSimpleName());
if (this.payload instanceof byte[]) {
sb.append("[payload byte[").append(((byte[]) this.payload).length).append("]]");
}
else {
sb.append("[payload=").append(this.payload).append("]");
}
sb.append("[headers=").append(this.headers).append("]");
return sb.toString();
}
}