Package com.storedobject.job
Class MessageSender<T extends Message>
java.lang.Object
com.storedobject.job.Job
com.storedobject.job.DaemonJob
com.storedobject.job.MessageSender<T>
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected boolean
Is it possible to send this message?final void
execute()
Execute the Job.protected int
Batch size - How many messages to be read for sending in one batch?protected int
Get the current credit balance.protected int
getDeliveryCost
(T message) Get the number of credits required to deliver this message.protected String
Additional condition to be be added when messages are read for sending.protected void
This method is invoked when this job is executed for the first time, and it will be invoked only once.static void
kick()
Kick all active "message senders" to indicate that some new messages are available for sending.protected boolean
Check if we need to monitor delivery cost or not.protected void
recover()
This method is invoked when the sender was stopped due to any error.protected abstract int
sendMessage
(T message) Send a message.protected boolean
sendMessages
(List<T> messages) Send a batch of messages.final void
shutdown()
This method is invoked when the system is shutting down.protected void
This method is invoked when the sender needs to be shut down.
-
Constructor Details
-
MessageSender
-
-
Method Details
-
initialize
protected void initialize()This method is invoked when this job is executed for the first time, and it will be invoked only once. You may set up the sender here. -
recover
protected void recover()This method is invoked when the sender was stopped due to any error. Make sure thatDaemonJob.isActive()
method returnsfalse
to indicate that the sender had stopped with some error condition. All recovery action should happen here and theDaemonJob.isActive()
method should returntrue
after this. Otherwise, no messages will be sent by invoking thesendMessage(Message)
method. -
execute
-
kick
public static void kick()Kick all active "message senders" to indicate that some new messages are available for sending. -
shutdown
-
terminate
protected void terminate()This method is invoked when the sender needs to be shut down. This will be invoked only once. -
getSelectCondition
Additional condition to be be added when messages are read for sending.- Returns:
- Default implementation returns null, so no additional selection criteria are applied.
-
monitorDeliveryCost
protected boolean monitorDeliveryCost()Check if we need to monitor delivery cost or not.Note: Applicable mostly to SMS messages.
- Returns:
- True/false.
-
getCreditBalance
protected int getCreditBalance()Get the current credit balance.Note: Applicable mostly to SMS messages.
- Returns:
- Current credit balance.
-
getDeliveryCost
Get the number of credits required to deliver this message.Note: Applicable mostly to SMS messages.
- Parameters:
message
- Message to send.- Returns:
- Cost (in number of credits).
-
sendMessages
Send a batch of messages. The default implementation of this method process each message by invokingsendMessage(Message)
and if this is overridden, thesendMessage(Message)
will not be invoked from elsewhere.Note: Status of the message should be updated adn saved within this method if overridden.
- Parameters:
messages
- Messages to be sent.- Returns:
- True if messages are sent successfully.
-
sendMessage
Send a message. The method should just send the message and should not update any status.- Parameters:
message
- Message to send.- Returns:
- The error code to indicate the status. 0: Successful, 1: Retry later, 2: Insufficient balance. Anything
above 2 indicates an error condition that doesn't allow this message to be sent (for example: invalid mobile
number in case of an
SMSMessage
).
-
getBatchSize
protected int getBatchSize()Batch size - How many messages to be read for sending in one batch?- Returns:
- Default is 50.
-
canSend
Is it possible to send this message?- Parameters:
message
- Message.- Returns:
- True/false.
-