Package com.yahoo.messagebus
Class SourceSession
- java.lang.Object
-
- com.yahoo.messagebus.SourceSession
-
- All Implemented Interfaces:
MessageBus.SendBlockedMessages
,ReplyHandler
public final class SourceSession extends java.lang.Object implements ReplyHandler, MessageBus.SendBlockedMessages
A session supporting sending new messages.- Author:
- Simon Thoresen Hult
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
Reject all new messages and wait until no messages are pending.boolean
destroy()
Sets the destroyed flag to true.int
getPendingCount()
Returns the number of messages sent that have not been replied to yetReplyHandler
getReplyHandler()
Returns the reply handler of this sessionvoid
handleReply(Reply reply)
This function is called when a reply arrives.Result
send(Message message)
Sends a new message.Result
send(Message msg, Route route)
This is a convenience function to assign a given route to the given message, and then pass it to the othersend(Message)
method of this session.Result
send(Message msg, java.lang.String routeName)
This is a convenience method to callsend(Message,String,boolean)
with afalse
value for the 'parseIfNotFound' parameter.Result
send(Message msg, java.lang.String routeName, boolean parseIfNotFound)
This is a convenience function to assign a named route to the given message, and then pass it to the othersend(Message)
method of this session.Result
sendBlocking(Message msg)
This is a blocking proxy to thesend(Message)
method.SourceSession
setTimeout(double timeout)
Sets the number of seconds a message can be attempted sent until it times out.boolean
trySend()
Do what you want, but dont block.
-
-
-
Method Detail
-
destroy
public boolean destroy()
Sets the destroyed flag to true. The very first time this method is called, it cleans up all its dependencies. Even if you retain a reference to this object, all of its content is allowed to be garbage collected.- Returns:
- true if content existed and was destroyed.
-
close
public void close()
Reject all new messages and wait until no messages are pending. Before returning, this method callsdestroy()
.
-
send
public Result send(Message message)
Sends a new message. Calling this immediately causes one of three possible results:
- A result is returned indicating that the message is accepted. In this case, a reply to the message is guaranteed to be produced on this session within a timeout limit. That reply may indicate either success or failure.
- A result is returned indicating that the message is not accepted. This is a transient failure, retrying the same operation after some wait period should cause it to be accepted.
- An exception is thrown, indicating a non-transient error which is not expected to be fixed before some corrective action is taken.
A source client should typically do some equivalent of:
do { Result result = sourceSession.send(message); if (!result.isAccepted()) // Do something else or wait a while } while (!result.isAccepted());
- Parameters:
message
- the message to send- Returns:
- the result of initiating sending of this message
-
trySend
public boolean trySend()
Description copied from interface:MessageBus.SendBlockedMessages
Do what you want, but dont block. You will be called regularly until you signal you are done- Specified by:
trySend
in interfaceMessageBus.SendBlockedMessages
- Returns:
- true unless you are done
-
sendBlocking
public Result sendBlocking(Message msg) throws java.lang.InterruptedException
This is a blocking proxy to thesend(Message)
method. This method blocks until the message is accepted by the send queue. Note that the message timeout does not activate by calling this method. This method will also return if this session is closed or the calling thread is interrupted.- Parameters:
msg
- the message to send- Returns:
- the result of initiating send
- Throws:
java.lang.InterruptedException
- thrown if the calling thread is interrupted
-
handleReply
public void handleReply(Reply reply)
Description copied from interface:ReplyHandler
This function is called when a reply arrives.- Specified by:
handleReply
in interfaceReplyHandler
- Parameters:
reply
- The reply that arrived.
-
send
public Result send(Message msg, Route route)
This is a convenience function to assign a given route to the given message, and then pass it to the othersend(Message)
method of this session.- Parameters:
msg
- the message to sendroute
- the route to assign to the message- Returns:
- the immediate result of the attempt to send this message
-
send
public Result send(Message msg, java.lang.String routeName)
This is a convenience method to callsend(Message,String,boolean)
with afalse
value for the 'parseIfNotFound' parameter.- Parameters:
msg
- the message to sendrouteName
- the route to assign to the message- Returns:
- the immediate result of the attempt to send this message
-
send
public Result send(Message msg, java.lang.String routeName, boolean parseIfNotFound)
This is a convenience function to assign a named route to the given message, and then pass it to the othersend(Message)
method of this session. If the route could not be found this methods returns with an appropriate error, unless the 'parseIfNotFound' argument is true. In that case, the route name is passed through to the Route factory methodRoute.parse(java.lang.String)
.- Parameters:
msg
- the message to sendrouteName
- the route to assign to the messageparseIfNotFound
- whether or not to parse routeName as a route if it could not be found- Returns:
- the immediate result of the attempt to send this message
-
getReplyHandler
public ReplyHandler getReplyHandler()
Returns the reply handler of this session
-
getPendingCount
public int getPendingCount()
Returns the number of messages sent that have not been replied to yet
-
setTimeout
public SourceSession setTimeout(double timeout)
Sets the number of seconds a message can be attempted sent until it times out.- Parameters:
timeout
- the number of seconds allowed.- Returns:
- this, to allow chaining.
-
-