📄 agateway.java
字号:
}
/**
* Sets the string that will appear on recipient's phone as the originator.
* Not all gateways support this.
*
* @param from
* The originator string.
* @see #getFrom()
*/
public void setFrom(String from)
{
this.from = from;
}
public boolean queueMessage(OutboundMessage msg)
{
if (msg.getPriority() == MessagePriorities.LOW) lowQ.add(msg);
else if (msg.getPriority() == MessagePriorities.NORMAL) normalQ.add(msg);
else if (msg.getPriority() == MessagePriorities.HIGH) highQ.add(msg);
return true;
}
public int queueMessages(List msgList)
{
int count = 0;
for (int i = 0, n = msgList.size(); i < n; i++)
if (queueMessage((OutboundMessage) msgList.get(i))) count++;
return count;
}
public void startGateway() throws TimeoutException, GatewayException, IOException, InterruptedException
{
started = true;
queueManagerThread = new Thread(new QueueManager());
queueManagerThread.start();
gatewayStatus = GatewayStatuses.OK;
}
public void stopGateway() throws TimeoutException, GatewayException, IOException, InterruptedException
{
started = false;
if (queueManagerThread != null)
{
queueManagerThread.interrupt();
try
{
queueManagerThread.join();
}
catch (InterruptedException e)
{
logInfo("Interrupted while waiting for gateway to stop.", e);
}
finally
{
queueManagerThread = null;
}
}
}
public void readMessages(List 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(List msgList) throws TimeoutException, GatewayException, IOException, InterruptedException
{
int cnt = 0;
for (int i = 0; i < msgList.size(); i++)
if (sendMessage((OutboundMessage) msgList.get(i))) 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 deliveryErrorCode;
}
boolean isCapableOf(int att)
{
return ((att & attributes) == att);
}
boolean conformsTo(int attrib, boolean required)
{
if (required && !isCapableOf(attrib)) return false;
else return true;
}
static class Statistics
{
public int inbound;
public int outbound;
public Statistics()
{
inbound = 0;
outbound = 0;
}
}
int getQueueLoad()
{
return (getQueueLoad(MessagePriorities.LOW) + getQueueLoad(MessagePriorities.NORMAL) + getQueueLoad(MessagePriorities.HIGH));
}
int getQueueLoad(MessagePriorities priority)
{
if (priority == MessagePriorities.LOW) return lowQ.size();
else if (priority == MessagePriorities.NORMAL) return normalQ.size();
else if (priority == MessagePriorities.HIGH) return highQ.size();
else return 0;
}
public void logError(String message)
{
srv.logError("GTW: " + gtwId + ": " + message, null);
}
public void logError(String message, Exception e)
{
srv.logError("GTW: " + gtwId + ": " + message, e);
}
public void logDebug(String message)
{
srv.logDebug("GTW: " + gtwId + ": " + message, null);
}
public void logDebug(String message, Exception e)
{
srv.logDebug("GTW: " + gtwId + ": " + message, e);
}
public void logWarn(String message)
{
srv.logWarn("GTW: " + gtwId + ": " + message, null);
}
public void logWarn(String message, Exception e)
{
srv.logWarn("GTW: " + gtwId + ": " + message, e);
}
public void logInfo(String message)
{
srv.logInfo("GTW: " + gtwId + ": " + message, null);
}
public void logInfo(String message, Exception e)
{
srv.logInfo("GTW: " + gtwId + ": " + message, e);
}
private class QueueManager implements Runnable
{
public QueueManager()
{
super();
}
public Object get()
{
if (highQ.size() > 0) return highQ.get();
else if (normalQ.size() > 0) return normalQ.get();
else if (lowQ.size() > 0) return lowQ.get();
else return null;
}
public void run()
{
OutboundMessage msg = null;
logInfo("Starting Queue Manager.", null);
try
{
if (started)
{
while (true)
{
while (true)
{
msg = (OutboundMessage) get();
if (msg == null) Thread.sleep(srv.S.QUEUE_INTERVAL);
else break;
}
if ((!started) || (gatewayStatus != GatewayStatuses.OK)) break;
if (msg != null)
{
if (!sendMessage(msg))
{
if (msg.getRetryCount() < srv.S.QUEUE_RETRIES)
{
logInfo("Reinserting message to queue.", null);
msg.incrementRetryCount();
queueMessage(msg);
}
else
{
logWarn("Maximum number of queue retries exceeded, message lost.", null);
msg.setFailureCause(FailureCauses.UNKNOWN);
if (getOutboundNotification() != null) getOutboundNotification().process(gtwId, msg);
}
}
else if (getOutboundNotification() != null) getOutboundNotification().process(gtwId, msg);
}
msg = null;
try
{
Thread.sleep(srv.S.QUEUE_INTERVAL);
}
catch (Exception e)
{
}
if (!started) break;
}
}
}
catch (InterruptedException e)
{
if ((msg != null) && (msg.getMessageStatus() != MessageStatuses.SENT)) queueMessage(msg);
logInfo("Interrupting queue.", e);
}
catch (Exception e)
{
logWarn("Queue exception, marking gateway for reset.", e);
gatewayStatus = GatewayStatuses.RESTART;
try
{
if ((msg != null) && (msg.getMessageStatus() != MessageStatuses.SENT)) queueMessage(msg);
}
catch (Exception e1)
{
logError("Fatal error during restart of the queue.", e1);
}
}
logInfo("QueueManager stopped.", null);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -