📄 agateway.java
字号:
@Deprecated
public void setStatusNotification(IGatewayStatusNotification myStatusNotification)
{
this.statusNotification = myStatusNotification;
}
/**
* Returns the total number of messages received by this gateway.
*
* @return The number of received messages.
*/
public int getInboundMessageCount()
{
return this.statistics.inbound;
}
public void incInboundMessageCount()
{
this.statistics.inbound++;
}
/**
* Returns the total number of messages sent via this gateway.
*
* @return The number of sent messages.
*/
public int getOutboundMessageCount()
{
return this.statistics.outbound;
}
public void incOutboundMessageCount()
{
this.statistics.outbound++;
}
/**
* Returns the string that will appear on recipient's phone as the
* originator. Not all gateways support this.
*
* @return The originator string.
* @see #setFrom(String)
*/
public String getFrom()
{
return this.from;
}
/**
* Sets the string that will appear on recipient's phone as the originator.
* Not all gateways support this.
*
* @param myFrom
* The originator string.
* @see #getFrom()
*/
public void setFrom(String myFrom)
{
this.from = myFrom;
}
public boolean queueMessage(OutboundMessage msg)
{
this.messageQueue.add(msg);
return true;
}
public int queueMessages(Collection<OutboundMessage> msgList)
{
int count = 0;
for (OutboundMessage msg : msgList)
if (queueMessage(msg)) count++;
return count;
}
public boolean unqueueMessage(OutboundMessage msg)
{
return this.messageQueue.remove(msg);
}
public void startGateway() throws TimeoutException, GatewayException, IOException, InterruptedException
{
this.queueManagerThread = new Thread(new QueueManager());
this.queueManagerThread.setName("SMSLib-QueueManager");
this.queueManagerThread.setDaemon(true);
this.queueManagerThread.start();
setStatus(GatewayStatuses.RUNNING);
this.restartCount ++;
}
public void stopGateway() throws TimeoutException, GatewayException, IOException, InterruptedException
{
setStatus(GatewayStatuses.STOPPED);
if (this.queueManagerThread != null)
{
this.queueManagerThread.interrupt();
try
{
this.queueManagerThread.join();
}
catch (InterruptedException e)
{
getService().getLogger().logInfo("Interrupted while waiting for gateway to stop.", e, getGatewayId());
}
finally
{
this.queueManagerThread = null;
}
}
}
public void readMessages(Collection<InboundMessage> msgList, MessageClasses msgClass) throws TimeoutException, GatewayException, IOException, InterruptedException
{
throw new GatewayException("Feature not supported.");
}
public InboundMessage readMessage(String memLoc, int memIndex) throws TimeoutException, GatewayException, IOException, InterruptedException
{
throw new GatewayException("Feature not supported.");
}
public boolean sendMessage(OutboundMessage msg) throws TimeoutException, GatewayException, IOException, InterruptedException
{
throw new GatewayException("Feature not supported.");
}
public int sendMessages(Collection<OutboundMessage> msgList) throws TimeoutException, GatewayException, IOException, InterruptedException
{
int cnt = 0;
for (OutboundMessage msg : msgList)
if (sendMessage(msg)) cnt++;
return cnt;
}
public boolean deleteMessage(InboundMessage msg) throws TimeoutException, GatewayException, IOException, InterruptedException
{
throw new GatewayException("Feature not supported.");
}
/**
* Queries the gateway for remaining credit.
*
* @return Remaining credit.
* @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 float queryBalance() throws TimeoutException, GatewayException, IOException, InterruptedException
{
throw new GatewayException("Feature not supported.");
}
/**
* Queries the gateway to see if a specific message and its recipient are
* covered. The given message is not sent out - it is just tested.
*
* @param msg
* The message to test.
* @return True is the recipient is covered by the network.
* @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 queryCoverage(OutboundMessage msg) throws TimeoutException, GatewayException, IOException, InterruptedException
{
throw new GatewayException("Feature not supported.");
}
/**
* Query the gateway for message delivery status.
*
* @param msg
* The OutboundMessage object to be checked.
* @return The delivery status. This is interpreted and mapped to the
* standard SMSLib status codes. For detailed information, check
* method getDeliveryErrorCode().
* @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 DeliveryStatuses
* @see #getDeliveryErrorCode()
*/
public DeliveryStatuses queryMessage(OutboundMessage msg) throws TimeoutException, GatewayException, IOException, InterruptedException
{
return queryMessage(msg.getRefNo());
}
/**
* Query the gateway for message delivery status.
*
* @param refNo
* The reference number of a previously sent message to be
* checked.
* @return The delivery status. This is interpreted and mapped to the
* standard SMSLib status codes. For detailed information, check
* method getDeliveryErrorCode().
* @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 DeliveryStatuses
* @see #getDeliveryErrorCode()
*/
public DeliveryStatuses queryMessage(String refNo) throws TimeoutException, GatewayException, IOException, InterruptedException
{
throw new GatewayException("Feature not supported.");
}
/**
* Returns the gateway-specific error code from the last queryMessage()
* call. Note that each call to queryMessage() resets this error.
*
* @return The error code - actual values depend on gateway used.
* @see #queryMessage(OutboundMessage)
*/
public int getDeliveryErrorCode()
{
return this.deliveryErrorCode;
}
public void setDeliveryErrorCode(int error)
{
this.deliveryErrorCode = error;
}
boolean isCapableOf(int att)
{
return ((att & this.attributes) == att);
}
boolean conformsTo(int attrib, boolean required)
{
if (required && !isCapableOf(attrib)) return false;
return true;
}
static class Statistics
{
public int inbound;
public int outbound;
public Statistics()
{
this.inbound = 0;
this.outbound = 0;
}
}
public int getQueueLoad()
{
return this.messageQueue.size();
}
public int getQueueLoad(int priority)
{
int count = 0;
for (OutboundMessage msg : this.messageQueue)
if (msg.getPriority() == priority) count++;
return count;
}
public int getRestartCount()
{
return restartCount;
}
private class QueueManager implements Runnable
{
public QueueManager()
{
super();
}
public void run()
{
OutboundMessage msg = null;
getService().getLogger().logInfo("Starting Queue Manager.", null, getGatewayId());
try
{
if (getStatus() == GatewayStatuses.RUNNING)
{
while (true)
{
msg = AGateway.this.messageQueue.take();
if (getStatus() != GatewayStatuses.RUNNING) break;
if (msg != null)
{
if (!sendMessage(msg))
{
if (msg.getRetryCount() < getService().S.QUEUE_RETRIES)
{
getService().getLogger().logInfo("Reinserting message to queue.", null, getGatewayId());
msg.incrementRetryCount();
queueMessage(msg);
}
else
{
getService().getLogger().logWarn("Maximum number of queue retries exceeded, message lost.", null, getGatewayId());
msg.setFailureCause(FailureCauses.UNKNOWN);
if (getOutboundNotification() != null) getOutboundNotification().process(getGatewayId(), msg);
if (getService().getOutboundNotification() != null) getService().getOutboundNotification().process(getGatewayId(), msg);
}
}
else
{
if (getOutboundNotification() != null) getOutboundNotification().process(getGatewayId(), msg);
if (getService().getOutboundNotification() != null) getService().getOutboundNotification().process(getGatewayId(), msg);
}
}
msg = null;
try
{
Thread.sleep(getService().S.QUEUE_INTERVAL);
}
catch (InterruptedException e)
{
// Swallow this...
}
if (getStatus() != GatewayStatuses.RUNNING) break;
}
}
}
catch (InterruptedException e)
{
if ((msg != null) && (msg.getMessageStatus() != MessageStatuses.SENT)) queueMessage(msg);
//getService().getLogger().logInfo("Interrupting queue.", e);
}
catch (Exception e)
{
getService().getLogger().logWarn("Queue exception, marking gateway for reset.", e, getGatewayId());
setStatus(GatewayStatuses.RESTART);
try
{
if ((msg != null) && (msg.getMessageStatus() != MessageStatuses.SENT)) queueMessage(msg);
}
catch (Exception e1)
{
getService().getLogger().logError("Fatal error during restart of the queue.", e1, getGatewayId());
}
}
getService().getLogger().logInfo("QueueManager stopped.", null, getGatewayId());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -