public class DefaultStompSession extends java.lang.Object implements ConnectionHandlingStompSession
ConnectionHandlingStompSession
.Modifier and Type | Class and Description |
---|---|
private class |
DefaultStompSession.DefaultSubscription |
private class |
DefaultStompSession.ReadInactivityTask |
private class |
DefaultStompSession.ReceiptHandler |
private class |
DefaultStompSession.WriteInactivityTask |
StompSession.Receiptable, StompSession.Subscription
Modifier and Type | Field and Description |
---|---|
private boolean |
autoReceiptEnabled |
private boolean |
closing |
private StompHeaders |
connectHeaders |
private TcpConnection<byte[]> |
connection |
private MessageConverter |
converter |
static byte[] |
EMPTY_PAYLOAD
An empty payload.
|
private static Message<byte[]> |
HEARTBEAT |
private static long |
HEARTBEAT_MULTIPLIER |
private static IdGenerator |
idGenerator |
private static Log |
logger |
private java.util.Map<java.lang.String,DefaultStompSession.ReceiptHandler> |
receiptHandlers |
private java.util.concurrent.atomic.AtomicInteger |
receiptIndex |
private long |
receiptTimeLimit |
private SettableListenableFuture<StompSession> |
sessionFuture |
private StompSessionHandler |
sessionHandler |
private java.lang.String |
sessionId |
private java.util.concurrent.atomic.AtomicInteger |
subscriptionIndex |
private java.util.Map<java.lang.String,DefaultStompSession.DefaultSubscription> |
subscriptions |
private TaskScheduler |
taskScheduler |
private java.lang.String |
version |
Constructor and Description |
---|
DefaultStompSession(StompSessionHandler sessionHandler,
StompHeaders connectHeaders)
Create a new session.
|
Modifier and Type | Method and Description |
---|---|
StompSession.Receiptable |
acknowledge(StompHeaders headers,
boolean consumed)
An overloaded version of
StompSession.acknowledge(String, boolean) with
full StompHeaders instead of just a messageId . |
StompSession.Receiptable |
acknowledge(java.lang.String messageId,
boolean consumed)
Send an acknowledgement whether a message was consumed or not resulting
in an ACK or NACK frame respectively.
|
void |
afterConnected(TcpConnection<byte[]> connection)
Invoked after a connection is successfully established.
|
void |
afterConnectFailure(java.lang.Throwable ex)
Invoked on failure to connect.
|
void |
afterConnectionClosed()
Invoked after the connection is closed.
|
private java.lang.String |
checkOrAddReceipt(StompHeaders headers) |
private StompHeaderAccessor |
createHeaderAccessor(StompCommand command) |
private Message<byte[]> |
createMessage(StompHeaderAccessor accessor,
java.lang.Object payload) |
void |
disconnect()
Disconnect the session by sending a DISCONNECT frame.
|
private void |
execute(Message<byte[]> message) |
MessageConverter |
getMessageConverter()
Return the configured
MessageConverter . |
long |
getReceiptTimeLimit()
Return the configured time limit before a receipt expires.
|
ListenableFuture<StompSession> |
getSessionFuture()
Return a future that will complete when the session is ready for use.
|
StompSessionHandler |
getSessionHandler()
Return the configured session handler.
|
java.lang.String |
getSessionId()
Return the id for the session.
|
TaskScheduler |
getTaskScheduler()
Return the configured TaskScheduler to use for receipt tracking.
|
void |
handleFailure(java.lang.Throwable ex)
Handle a failure on the connection.
|
void |
handleMessage(Message<byte[]> message)
Handle a message received from the remote host.
|
private void |
initHeartbeatTasks(StompHeaders connectedHeaders) |
private void |
invokeHandler(StompFrameHandler handler,
Message<byte[]> message,
StompHeaders headers) |
boolean |
isAutoReceiptEnabled()
Whether receipt headers should be automatically added.
|
boolean |
isConnected()
Whether the session is connected.
|
private boolean |
isEmpty(java.lang.Object payload) |
private void |
resetConnection() |
StompSession.Receiptable |
send(StompHeaders headers,
java.lang.Object payload)
An overloaded version of
StompSession.send(String, Object) with full
StompHeaders instead of just a destination. |
StompSession.Receiptable |
send(java.lang.String destination,
java.lang.Object payload)
Send a message to the specified destination, converting the payload to a
byte[] with the help of a
MessageConverter . |
void |
setAutoReceipt(boolean autoReceiptEnabled)
When enabled, a receipt header is automatically added to future
send and subscribe operations on this session, which
causes the server to return a RECEIPT. |
void |
setMessageConverter(MessageConverter messageConverter)
Set the
MessageConverter to use to convert the payload of incoming
and outgoing messages to and from byte[] based on object type, or
expected object type, and the "content-type" header. |
void |
setReceiptTimeLimit(long receiptTimeLimit)
Configure the time in milliseconds before a receipt expires.
|
void |
setTaskScheduler(TaskScheduler taskScheduler)
Configure the TaskScheduler to use for receipt tracking.
|
StompSession.Subscription |
subscribe(StompHeaders headers,
StompFrameHandler handler)
An overloaded version of
StompSession.subscribe(String, StompFrameHandler)
with full StompHeaders instead of just a destination. |
StompSession.Subscription |
subscribe(java.lang.String destination,
StompFrameHandler handler)
Subscribe to the given destination by sending a SUBSCRIBE frame and handle
received messages with the specified
StompFrameHandler . |
private void |
unsubscribe(java.lang.String id,
StompHeaders headers) |
private static final Log logger
private static final IdGenerator idGenerator
public static final byte[] EMPTY_PAYLOAD
private static final long HEARTBEAT_MULTIPLIER
private static final Message<byte[]> HEARTBEAT
private final java.lang.String sessionId
private final StompSessionHandler sessionHandler
private final StompHeaders connectHeaders
private final SettableListenableFuture<StompSession> sessionFuture
private MessageConverter converter
@Nullable private TaskScheduler taskScheduler
private long receiptTimeLimit
private volatile boolean autoReceiptEnabled
@Nullable private volatile TcpConnection<byte[]> connection
@Nullable private volatile java.lang.String version
private final java.util.concurrent.atomic.AtomicInteger subscriptionIndex
private final java.util.Map<java.lang.String,DefaultStompSession.DefaultSubscription> subscriptions
private final java.util.concurrent.atomic.AtomicInteger receiptIndex
private final java.util.Map<java.lang.String,DefaultStompSession.ReceiptHandler> receiptHandlers
private volatile boolean closing
public DefaultStompSession(StompSessionHandler sessionHandler, StompHeaders connectHeaders)
sessionHandler
- the application handler for the sessionconnectHeaders
- headers for the STOMP CONNECT framepublic java.lang.String getSessionId()
StompSession
getSessionId
in interface StompSession
public StompSessionHandler getSessionHandler()
public ListenableFuture<StompSession> getSessionFuture()
ConnectionHandlingStompSession
getSessionFuture
in interface ConnectionHandlingStompSession
public void setMessageConverter(MessageConverter messageConverter)
MessageConverter
to use to convert the payload of incoming
and outgoing messages to and from byte[]
based on object type, or
expected object type, and the "content-type" header.
By default, SimpleMessageConverter
is configured.
messageConverter
- the message converter to usepublic MessageConverter getMessageConverter()
MessageConverter
.public void setTaskScheduler(@Nullable TaskScheduler taskScheduler)
@Nullable public TaskScheduler getTaskScheduler()
public void setReceiptTimeLimit(long receiptTimeLimit)
By default set to 15,000 (15 seconds).
public long getReceiptTimeLimit()
public void setAutoReceipt(boolean autoReceiptEnabled)
StompSession
send
and subscribe
operations on this session, which
causes the server to return a RECEIPT. An application can then use
the Receiptable
returned from the
operation to track the receipt.
A receipt header can also be added manually through the overloaded
methods that accept StompHeaders
.
setAutoReceipt
in interface StompSession
public boolean isAutoReceiptEnabled()
public boolean isConnected()
StompSession
isConnected
in interface StompSession
public StompSession.Receiptable send(java.lang.String destination, java.lang.Object payload)
StompSession
byte[]
with the help of a
MessageConverter
.send
in interface StompSession
destination
- the destination to send a message topayload
- the message payloadpublic StompSession.Receiptable send(StompHeaders headers, java.lang.Object payload)
StompSession
StompSession.send(String, Object)
with full
StompHeaders
instead of just a destination. The headers must
contain a destination and may also have other headers such as
"content-type" or custom headers for the broker to propagate to
subscribers, or broker-specific, non-standard headers.send
in interface StompSession
headers
- the message headerspayload
- the message payload@Nullable private java.lang.String checkOrAddReceipt(StompHeaders headers)
private StompHeaderAccessor createHeaderAccessor(StompCommand command)
private Message<byte[]> createMessage(StompHeaderAccessor accessor, @Nullable java.lang.Object payload)
private boolean isEmpty(@Nullable java.lang.Object payload)
private void execute(Message<byte[]> message)
public StompSession.Subscription subscribe(java.lang.String destination, StompFrameHandler handler)
StompSession
StompFrameHandler
.subscribe
in interface StompSession
destination
- the destination to subscribe tohandler
- the handler for received messagespublic StompSession.Subscription subscribe(StompHeaders headers, StompFrameHandler handler)
StompSession
StompSession.subscribe(String, StompFrameHandler)
with full StompHeaders
instead of just a destination.subscribe
in interface StompSession
headers
- the headers for the subscribe message framehandler
- the handler for received messagespublic StompSession.Receiptable acknowledge(java.lang.String messageId, boolean consumed)
StompSession
Note: to use this when subscribing you must set the
ack
header to "client" or
"client-individual" in order ot use this.
acknowledge
in interface StompSession
messageId
- the id of the messageconsumed
- whether the message was consumed or notpublic StompSession.Receiptable acknowledge(StompHeaders headers, boolean consumed)
StompSession
StompSession.acknowledge(String, boolean)
with
full StompHeaders
instead of just a messageId
.acknowledge
in interface StompSession
headers
- the headers for the ACK or NACK message frameconsumed
- whether the message was consumed or notprivate void unsubscribe(java.lang.String id, @Nullable StompHeaders headers)
public void disconnect()
StompSession
disconnect
in interface StompSession
public void afterConnected(TcpConnection<byte[]> connection)
TcpConnectionHandler
afterConnected
in interface TcpConnectionHandler<byte[]>
connection
- the connectionpublic void afterConnectFailure(java.lang.Throwable ex)
TcpConnectionHandler
afterConnectFailure
in interface TcpConnectionHandler<byte[]>
ex
- the exceptionpublic void handleMessage(Message<byte[]> message)
TcpConnectionHandler
handleMessage
in interface TcpConnectionHandler<byte[]>
message
- the messageprivate void invokeHandler(StompFrameHandler handler, Message<byte[]> message, StompHeaders headers)
private void initHeartbeatTasks(StompHeaders connectedHeaders)
public void handleFailure(java.lang.Throwable ex)
TcpConnectionHandler
handleFailure
in interface TcpConnectionHandler<byte[]>
ex
- the exceptionpublic void afterConnectionClosed()
TcpConnectionHandler
afterConnectionClosed
in interface TcpConnectionHandler<byte[]>
private void resetConnection()