Allow to set/get an attachment on a Channel without the need of

ChannelLocal. See #101
This commit is contained in:
norman 2011-12-07 07:54:15 +01:00
parent a9a713ddcf
commit cc761ae786
3 changed files with 31 additions and 1 deletions

View File

@ -57,7 +57,8 @@ public abstract class AbstractChannel implements Channel {
/** Cache for the string representation of this channel */
private boolean strValConnected;
private String strVal;
private volatile Object attachment;
/**
* Creates a new instance.
*
@ -272,6 +273,16 @@ public abstract class AbstractChannel implements Channel {
return Channels.write(this, message, remoteAddress);
}
@Override
public Object getAttachment() {
return attachment;
}
@Override
public void setAttachment(Object attachment) {
this.attachment = attachment;
}
/**
* Returns the {@link String} representation of this channel. The returned
* string contains the {@linkplain #getId() ID}, {@linkplain #getLocalAddress() local address},

View File

@ -362,4 +362,19 @@ public interface Channel extends Comparable<Channel> {
* {@code interestOps} change request succeeds or fails
*/
ChannelFuture setReadable(boolean readable);
/**
* Retrieves an object which is {@link #setAttachment(Object) attached} to
* this {@link Channel}.
*
* @return {@code null} if no object was attached or
* {@code null} was attached
*/
Object getAttachment();
/**
* Attaches an object to this {@link Channel} to store a stateful information
*
*/
void setAttachment(Object attachment);
}

View File

@ -32,8 +32,12 @@ import org.jboss.netty.util.internal.ConcurrentIdentityWeakKeyHashMap;
* @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
* @author <a href="http://gleamynode.net/">Trustin Lee</a>
*
* @deprecated Use {@link Channel#setAttachment(Object)} and {@link Channel#getAttachment()}
*
* @apiviz.stereotype utility
*
*/
@Deprecated
public class ChannelLocal<T> {
private final ConcurrentMap<Channel, T> map =