📄 service.java
字号:
*
* @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);
}
}
public Queue<OutboundMessage> getReorderMessageQueue()
{
return this.reorderMessageQueue;
}
/**
* Returns the notification method set for inbound messages. Returns null if
* no such method is set.
*
* @return The notification method.
* @see #setInboundNotification(IInboundMessageNotification)
*/
public IInboundMessageNotification getInboundNotification()
{
return this.inboundNotification;
}
/**
* Sets the inbound message notification method. The method must adhere to
* the IInboundMessageNotification interface. If set, SMSLib will call this
* method upon arrival of a new inbound message.
*
* @param myInboundNotification
* The method to be called.
* @see #getInboundNotification()
* @see IInboundMessageNotification
*/
public void setInboundNotification(IInboundMessageNotification myInboundNotification)
{
this.inboundNotification = myInboundNotification;
}
/**
* Returns the notification method set for outbound messages. Returns null
* if no such method is set.
*
* @return The notification method.
* @see #setOutboundNotification(IOutboundMessageNotification)
*/
public IOutboundMessageNotification getOutboundNotification()
{
return this.outboundNotification;
}
/**
* Sets the outbound notification method. The method must adhere to the
* IOutboundMessageNotification interface. If set, SMSLib will call this
* method upon dispatch of a message through the queueing (asyncronous)
* calls.
*
* @param myOutboundNotification
* @see #getOutboundNotification()
* @see IOutboundMessageNotification
*/
public void setOutboundNotification(IOutboundMessageNotification myOutboundNotification)
{
this.outboundNotification = myOutboundNotification;
}
/**
* Returns the call notification method. Returns null if no such method is
* set.
*
* @return The notification method.
* @see #setCallNotification(ICallNotification)
*/
public ICallNotification getCallNotification()
{
return this.callNotification;
}
/**
* Sets the call notification method. The method must adhere to the
* ICallNotification interface. If set, SMSLib will call this method upon
* detection of an inbound call.
*
* @param myCallNotification
* @see #getCallNotification()
* @see ICallNotification
*/
public void setCallNotification(ICallNotification myCallNotification)
{
this.callNotification = myCallNotification;
}
/**
* Returns the gateway status notification method. Returns null if no such
* method has been set.
*
* @return The notification method.
* @see #setGatewayStatusNotification(IGatewayStatusNotification)
*/
public IGatewayStatusNotification getGatewayStatusNotification()
{
return this.gatewayStatusNotification;
}
/**
* Sets the gateway status notification method. The method must adhere to
* the IGatewayStatusNotification interface. If set, SMSLib will call this
* method upon every gateway status change.
*
* @param myGatewayStatusNotification
* @see #getGatewayStatusNotification()
* @see IGatewayStatusNotification
*/
public void setGatewayStatusNotification(IGatewayStatusNotification myGatewayStatusNotification)
{
this.gatewayStatusNotification = myGatewayStatusNotification;
}
public long getStartMillis()
{
return this.startMillis;
}
private class WatchDog extends Thread
{
public WatchDog()
{
setName("SMSLib-WatchDog");
setDaemon(true);
start();
}
@Override
public void run()
{
boolean needShaking = false;
boolean drainAll = false;
getLogger().logDebug("WatchDog started.", null, null);
while (true)
{
try
{
getLogger().logInfo("WatchDog running...", null, null);
for (AGateway gateway : getGateways())
{
if (gateway.getStatus() == GatewayStatuses.RESTART)
{
if (gateway.getQueueLoad() != 0)
{
getLogger().logDebug("Draining messages: Gateway: " + gateway.getGatewayId() + " restarting...", null, null);
synchronized (getReorderMessageQueue())
{
synchronized (gateway.messageQueue)
{
gateway.messageQueue.drainTo(getReorderMessageQueue());
}
}
needShaking = true;
}
getLogger().logWarn("WatchDog: Gateway: " + gateway.getGatewayId() + " restarting.", null, null);
try
{
gateway.stopGateway();
gateway.setStatus(GatewayStatuses.RESTART);
}
catch (Exception e)
{
getLogger().logWarn("WatchDog: error while shutting down Gateway: " + gateway.getGatewayId(), e, null);
}
try
{
gateway.startGateway();
}
catch (Exception e)
{
getLogger().logError("WatchDog: error while starting Gateway: " + gateway.getGatewayId(), e, null);
}
if (gateway.getStatus() == GatewayStatuses.RUNNING)
{
needShaking = true;
drainAll = true;
}
}
}
if (needShaking == true)
{
shakeMessages(drainAll);
needShaking = false;
drainAll = false;
}
Thread.sleep(Service.this.S.WATCHDOG_INTERVAL);
}
catch (InterruptedException e)
{
break;
}
catch (Exception e)
{
getLogger().logError("WatchDog error. ", e, null);
}
}
getLogger().logDebug("WatchDog stopped.", null, null);
}
/**
* Queues all pending messages again
*
* @param drainAll
* if <b>true</b> then prior to queueing pending messages all
* message queues of all gateways will be drained to pending
* messages list
*/
private void shakeMessages(boolean drainAll)
{
if (drainAll)
{
for (AGateway gtw : getGateways())
{
synchronized (gtw.messageQueue)
{
gtw.messageQueue.drainTo(getReorderMessageQueue());
}
}
}
for (OutboundMessage msg : getReorderMessageQueue())
{
if (!queueMessage(msg)) if (getOutboundNotification() != null) getOutboundNotification().process(null, msg);
}
getReorderMessageQueue().clear();
}
}
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 + -