📄 service.java
字号:
* 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.
*/
@SuppressWarnings("unused")
public boolean deleteMessage(InboundMessage msg) throws TimeoutException, GatewayException, IOException, InterruptedException
{
synchronized (getGateways())
{
AGateway gateway = findGateway(msg.getGatewayId());
if (gateway != null)
{
try
{
return gateway.deleteMessage(msg);
}
catch (TimeoutException e)
{
getLogger().logWarn("deleteMessage(): Gateway " + gateway.getGatewayId() + " does not respond, marking for restart.");
gateway.setGatewayStatus(GatewayStatuses.RESTART);
return false;
}
catch (IOException e)
{
getLogger().logWarn("deleteMessage(): Gateway " + gateway.getGatewayId() + " throws IO errors, marking for restart.");
gateway.setGatewayStatus(GatewayStatuses.RESTART);
return false;
}
}
return false;
}
}
/**
* Returns the total number of messages received by the specified gateway.
*
* @param gatewayId
* The id of the gateway to query.
* @return The number of received messages or -1 on error.
*/
public int getInboundMessageCount(String gatewayId)
{
return getInboundMessageCount(findGateway(gatewayId));
}
/**
* Returns the total number of messages received by the specified gateway.
*
* @param gateway
* The AGateway object to query.
* @return The number of received messages or -1 on error.
*/
public int getInboundMessageCount(AGateway gateway)
{
return (gateway != null ? gateway.getInboundMessageCount() : -1);
}
/**
* Returns the total number of messages sent via the specified gateway.
*
* @param gatewayId
* The id of the gateway to query.
* @return The number of sent messages or -1 on error.
*/
public int getOutboundMessageCount(String gatewayId)
{
return getOutboundMessageCount(findGateway(gatewayId));
}
/**
* Returns the total number of messages sent via the specified gateway.
*
* @param gateway
* The AGateway object to query.
* @return The number of sent messages or -1 on error.
*/
public int getOutboundMessageCount(AGateway gateway)
{
return (gateway != null ? gateway.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 (AGateway gateway : getGateways())
total += gateway.getInboundMessageCount();
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 (AGateway gateway : getGateways())
total += gateway.getOutboundMessageCount();
return total;
}
/**
* Find and return a gateway by its ID.
*
* @param gatewayId
* The ID of gateway to find.
* @return Gateway object bearing given name, or NULL if not found.
*/
public AGateway findGateway(String gatewayId)
{
for (AGateway gateway : getGateways())
if (gateway.getGatewayId().equals(gatewayId)) return gateway;
return null;
}
/**
* Returns the list of defined gateways.
*
* @return The list of gateways.
*/
public Collection<AGateway> getGateways()
{
return this.gatewayList;
}
/**
* 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(int)
* @see #getGatewayQueueLoad(String)
* @see #getGatewayQueueLoad(String, int)
*/
public int getGatewayQueueLoad()
{
int total = 0;
for (AGateway gateway : getGateways())
total += gateway.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, int)
*/
public int getGatewayQueueLoad(int priority)
{
int total = 0;
for (AGateway gateway : getGateways())
total += gateway.getQueueLoad(priority);
return total;
}
/**
* Retrieves the Queue load (i.e. pending messages) from a specific gateway
* and for all priorities.
*
* @param gatewayId
* The Gateway ID for which information is to be retrieved.
* @return The number of pending messages to be send.
* @see #getGatewayQueueLoad()
* @see #getGatewayQueueLoad(int)
* @see #getGatewayQueueLoad(String, int)
*/
public int getGatewayQueueLoad(String gatewayId)
{
AGateway gateway = findGateway(gatewayId);
return (gateway == null ? 0 : gateway.getQueueLoad());
}
/**
* Retrieves the Queue load (i.e. pending messages) from a specific gateway
* and for a specific priority.
*
* @param gatewayId
* 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(int)
* @see #getGatewayQueueLoad(String)
*/
public int getGatewayQueueLoad(String gatewayId, int priority)
{
AGateway gateway = findGateway(gatewayId);
return (gateway == null ? 0 : gateway.getQueueLoad(priority));
}
/**
* Returns the active Load Balancer class.
*
* @return The active LoadBalancer class.
* @see LoadBalancer
*/
public LoadBalancer getLoadBalancer()
{
return this.loadBalancer;
}
/**
* Sets a new Load Balancer.
*
* @param myLoadBalancer
* The Load Balancer that will take effect.
* @see LoadBalancer
*/
public void setLoadBalancer(LoadBalancer myLoadBalancer)
{
this.loadBalancer = myLoadBalancer;
}
/**
* Returns the active Router class.
*
* @return The active Router class.
* @see Router
*/
public Router getRouter()
{
return this.router;
}
/**
* Sets a new Router.
*
* @param myRouter
* The Router that will take effect.
* @see Router
*/
public void setRouter(Router myRouter)
{
this.router = myRouter;
}
/**
* 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 (getGateways())
{
return getRouter().route(msg);
}
}
private class WatchDog extends Thread
{
public WatchDog()
{
start();
}
@Override
public void run()
{
getLogger().logDebug("WatchDog started.");
while (true)
{
try
{
getLogger().logInfo("WatchDog running...");
for (AGateway gateway : getGateways())
{
if (gateway.getGatewayStatus() == GatewayStatuses.RESTART)
{
getLogger().logWarn("WatchDog: Gateway: " + gateway.getGatewayId() + " restarting.");
try
{
gateway.stopGateway();
}
catch (Exception e)
{
getLogger().logWarn("WatchDog: error while shutting down Gateway: " + gateway.getGatewayId(), e);
}
try
{
gateway.startGateway();
}
catch (Exception e)
{
getLogger().logError("WatchDog: error while starting Gateway: " + gateway.getGatewayId(), e);
}
}
}
Thread.sleep(Service.this.S.WATCHDOG_INTERVAL);
}
catch (InterruptedException e)
{
break;
}
catch (Exception e)
{
getLogger().logError("WatchDog error. ", e);
}
}
getLogger().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 + -