📄 service.java
字号:
public int sendMessages(List msgList) throws TimeoutException, GatewayException, IOException, InterruptedException
{
int counter = 0;
for (int i = 0; i < msgList.size(); i++)
{
OutboundMessage msg = (OutboundMessage) msgList.get(i);
if (sendMessage(msg)) counter++;
}
return counter;
}
/**
* Sends a list of messages from the specified gateway.
*
* @param msgList
* A list of OutboundMessage objects.
* @param gtwId
* The id of the gateway that will be used for sending.
* @return The number of messages sent.
* @throws TimeoutException
* The gateway did not respond in a timely manner.
* @throws GatewayException
* A Gateway error occurred.
* @throws IOException
* An IO error occurred.
* @throws InterruptedException
* The call was interrupted.
* @see #sendMessage(OutboundMessage)
*/
public int sendMessages(List msgList, String gtwId) throws TimeoutException, GatewayException, IOException, InterruptedException
{
int counter = 0;
for (int i = 0; i < msgList.size(); i++)
{
OutboundMessage msg = (OutboundMessage) msgList.get(i);
msg.setGatewayId(gtwId);
if (sendMessage(msg)) counter++;
}
return counter;
}
/**
* Queues a message for sending. The gateway selection logic is the same as
* for sendMessage(). The method does not block - returns immediately. If
* you wish to be alerted about the fate of the message, you may implement a
* IOutboundMessageNotification listener.
*
* @param msg
* Message to be sent
* @return True if the message is accepted in the Queue.
* @see #sendMessage(OutboundMessage)
* @see IOutboundMessageNotification
*/
public boolean queueMessage(OutboundMessage msg)
{
synchronized (gtwList)
{
AGateway gtw = routeMessage(msg);
if (gtw != null) return gtw.queueMessage(msg);
else return false;
}
}
/**
* Queues a message for sending from the specific gateway.
*
* @param msg
* A OutboundMessage object.
* @param gtwId
* The id of the gateway that will be used for sending.
* @return True if the message is accepted in the Queue.
* @see #queueMessage(OutboundMessage)
*/
public boolean queueMessage(OutboundMessage msg, String gtwId)
{
synchronized (gtwList)
{
msg.setGatewayId(gtwId);
return queueMessage(msg);
}
}
/**
* Queues a list of messages for sending.
*
* @param msgList
* A list of OutboundMessage objects.
* @return The number of messages accepted in the Queue.
* @see #queueMessage(OutboundMessage)
*/
public int queueMessages(List msgList)
{
int counter = 0;
for (int i = 0; i < msgList.size(); i++)
{
OutboundMessage msg = (OutboundMessage) msgList.get(i);
if (queueMessage(msg)) counter++;
}
return counter;
}
/**
* Queues a list of messages for sending from the specific gateway.
*
* @param msgList
* A list of OutboundMessage objects.
* @param gtwId
* The id of the gateway to be used for sending.
* @return The number of messages accepted in the Queue.
* @see #queueMessage(OutboundMessage)
*/
public int queueMessages(List msgList, String gtwId)
{
int counter = 0;
for (int i = 0; i < msgList.size(); i++)
{
OutboundMessage msg = (OutboundMessage) msgList.get(i);
msg.setGatewayId(gtwId);
if (queueMessage(msg)) counter++;
}
return counter;
}
/**
* Deletes the specified message. The operation is not supported by all
* gateways.
*
* @param msg
* The message to be deleted. It must be a valid InboundMessage
* object. <b>DO NOT PASS invalid objects to the method!</b>
* @return True if the message is deleted.
* @throws TimeoutException
* The gateway did not respond in a timely manner.
* @throws GatewayException
* A Gateway error occurred.
* @throws IOException
* An IO error occurred.
* @throws InterruptedException
* The call was interrupted.
*/
public boolean deleteMessage(InboundMessage msg) throws TimeoutException, GatewayException, IOException, InterruptedException
{
synchronized (gtwList)
{
AGateway gtw = findGateway(msg.getGatewayId());
if (gtw != null)
{
try
{
return gtw.deleteMessage(msg);
}
catch (TimeoutException e)
{
logWarn("deleteMessage(): Gateway " + gtw.getGatewayId() + " does not respond, marking for restart.");
gtw.setGatewayStatus(GatewayStatuses.RESTART);
return false;
}
catch (IOException e)
{
logWarn("deleteMessage(): Gateway " + gtw.getGatewayId() + " throws IO errors, marking for restart.");
gtw.setGatewayStatus(GatewayStatuses.RESTART);
return false;
}
}
else return false;
}
}
/**
* Returns the total number of messages received by the specified gateway.
*
* @param gtwId
* The id of the gateway to query.
* @return The number of received messages or -1 on error.
*/
public int getInboundMessageCount(String gtwId)
{
return getInboundMessageCount(findGateway(gtwId));
}
/**
* Returns the total number of messages received by the specified gateway.
*
* @param gtw
* The AGateway object to query.
* @return The number of received messages or -1 on error.
*/
public int getInboundMessageCount(AGateway gtw)
{
return (gtw != null ? gtw.getInboundMessageCount() : -1);
}
/**
* Returns the total number of messages sent via the specified gateway.
*
* @param gtwId
* The id of the gateway to query.
* @return The number of sent messages or -1 on error.
*/
public int getOutboundMessageCount(String gtwId)
{
return getOutboundMessageCount(findGateway(gtwId));
}
/**
* Returns the total number of messages sent via the specified gateway.
*
* @param gtw
* The AGateway object to query.
* @return The number of sent messages or -1 on error.
*/
public int getOutboundMessageCount(AGateway gtw)
{
return (gtw != null ? gtw.getOutboundMessageCount() : -1);
}
/**
* Returns the total number of messages received by all gateways.
*
* @return The number of received messages.
*/
public int getInboundMessageCount()
{
int total = 0;
for (int i = 0, n = gtwList.size(); i < n; i++)
total = total + getInboundMessageCount((AGateway) gtwList.get(i));
return total;
}
/**
* Returns the total number of messages sent via all gateways.
*
* @return The number of sent messages.
*/
public int getOutboundMessageCount()
{
int total = 0;
for (int i = 0, n = gtwList.size(); i < n; i++)
total = total + getOutboundMessageCount((AGateway) gtwList.get(i));
return total;
}
/**
* Find and return a gateway by its ID.
*
* @param gtwId
* The ID of gateway to find.
* @return Gateway object bearing given name, or NULL if not found.
*/
public AGateway findGateway(String gtwId)
{
for (int i = 0, n = gtwList.size(); i < n; i++)
if (((AGateway) gtwList.get(i)).getGatewayId().equals(gtwId)) return (AGateway) gtwList.get(i);
return null;
}
/**
* Returns the list of defined gateways.
*
* @return The list of gateways.
*/
public List getGatewayList()
{
return gtwList;
}
/**
* Retrieves the Queue load (i.e. pending messages) from all gateways and
* for all priorities.
*
* @return The number of pending messages to be send.
* @see #getGatewayQueueLoad(MessagePriorities)
* @see #getGatewayQueueLoad(String)
* @see #getGatewayQueueLoad(String, MessagePriorities)
*/
public int getGatewayQueueLoad()
{
int total = 0;
for (int i = 0, n = gtwList.size(); i < n; i++)
total += ((AGateway) gtwList.get(i)).getQueueLoad();
return total;
}
/**
* Retrieves the Queue load (i.e. pending messages) from all gateways and
* for a specific priority.
*
* @param priority
* The priority looked for.
* @return The number of pending messages to be send.
* @see #getGatewayQueueLoad()
* @see #getGatewayQueueLoad(String)
* @see #getGatewayQueueLoad(String, MessagePriorities)
*/
public int getGatewayQueueLoad(MessagePriorities priority)
{
int total = 0;
for (int i = 0, n = gtwList.size(); i < n; i++)
total += ((AGateway) gtwList.get(i)).getQueueLoad(priority);
return total;
}
/**
* Retrieves the Queue load (i.e. pending messages) from a specific gateway
* and for all priorities.
*
* @param gtwId
* The Gateway ID for which information is to be retrieved.
* @return The number of pending messages to be send.
* @see #getGatewayQueueLoad()
* @see #getGatewayQueueLoad(MessagePriorities)
* @see #getGatewayQueueLoad(String, MessagePriorities)
*/
public int getGatewayQueueLoad(String gtwId)
{
AGateway gtw = findGateway(gtwId);
return (gtw == null ? 0 : gtw.getQueueLoad());
}
/**
* Retrieves the Queue load (i.e. pending messages) from a specific gateway
* and for a specific priority.
*
* @param gtwId
* The Gateway ID for which information is to be retrieved.
* @param priority
* The priority looked for.
* @return The number of pending messages to be send.
* @see #getGatewayQueueLoad()
* @see #getGatewayQueueLoad(MessagePriorities)
* @see #getGatewayQueueLoad(String)
*/
public int getGatewayQueueLoad(String gtwId, MessagePriorities priority)
{
AGateway gtw = findGateway(gtwId);
return (gtw == null ? 0 : gtw.getQueueLoad(priority));
}
/**
* Returns the active Load Balancer class.
*
* @return The active LoadBalancer class.
* @see LoadBalancer
*/
public LoadBalancer getLoadBalancer()
{
return loadBalancer;
}
/**
* Sets a new Load Balancer.
*
* @param loadBalancer
* The Load Balancer that will take effect.
* @see LoadBalancer
*/
public void setLoadBalancer(LoadBalancer loadBalancer)
{
this.loadBalancer = loadBalancer;
}
/**
* Returns the active Router class.
*
* @return The active Router class.
* @see Router
*/
public Router getRouter()
{
return router;
}
/**
* Sets a new Router.
*
* @param router
* The Router that will take effect.
* @see Router
*/
public void setRouter(Router router)
{
this.router = router;
}
/**
* Find best suitable gateway to send specific message through Router and
* Load Balancer.
*
* @param msg
* Message to be routed
* @return Reference to gateway or <code>null</code> if no suitable
* gateway is found.
*/
AGateway routeMessage(OutboundMessage msg)
{
synchronized (gtwList)
{
return router.route(msg);
}
}
public void logError(String message)
{
logError(message, null);
}
public void logError(String message, Exception e)
{
logger.error(message + (e == null ? "" : (" (" + e.getMessage() + ")")));
}
public void logDebug(String message)
{
logDebug(message, null);
}
public void logDebug(String message, Exception e)
{
logger.debug(message + (e == null ? "" : (" (" + e.getMessage() + ")")));
}
public void logWarn(String message)
{
logWarn(message, null);
}
public void logWarn(String message, Exception e)
{
logger.warn(message + (e == null ? "" : (" (" + e.getMessage() + ")")));
}
public void logInfo(String message)
{
logInfo(message, null);
}
public void logInfo(String message, Exception e)
{
logger.info(message + (e == null ? "" : (" (" + e.getMessage() + ")")));
}
private class WatchDog extends Thread
{
public WatchDog()
{
start();
}
public void run()
{
logDebug("WatchDog started.");
while (true)
{
try
{
logInfo("WatchDog running...");
synchronized (gtwList)
{
for (int i = 0, n = gtwList.size(); i < n; i++)
{
AGateway gtw = (AGateway) gtwList.get(i);
if (gtw.getGatewayStatus() == GatewayStatuses.RESTART)
{
logWarn("WatchDog: Gateway: " + gtw.getGatewayId() + " restarting.");
try
{
gtw.stopGateway();
}
catch (Exception e)
{
logWarn("WatchDog: error while shutting down Gateway: " + gtw.getGatewayId(), e);
}
try
{
gtw.startGateway();
}
catch (Exception e)
{
logError("WatchDog: error while starting Gateway: " + gtw.getGatewayId(), e);
}
}
}
}
Thread.sleep(S.WATCHDOG_INTERVAL);
}
catch (InterruptedException e)
{
break;
}
catch (Exception e)
{
logError("WatchDog error. ", e);
}
}
logDebug("WatchDog stopped.");
}
}
public static void main(String[] args)
{
System.out.println(Library.getLibraryDescription());
System.out.println("\nSMSLib API Version: " + Library.getLibraryVersion());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -