⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 service.java

📁 发送短信 接收短信 多种接口com/net/modem 开发库
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
	 * .NET bridge method.
	 */
	public InboundMessage[] readMessages(MessageClasses msgClass, AGateway gateway) throws TimeoutException, GatewayException, IOException, InterruptedException
	{
		ArrayList<InboundMessage> messageList = new ArrayList<InboundMessage>();
		readMessages(messageList, msgClass, gateway);
		return messageList.toArray(new InboundMessage[0]);
	}

	/**
	 * Reads a specific gateway for a message matching the given Memory Location
	 * and Memory Index.
	 * <p>
	 * This is a "dummy" approach. It does not implement the CGMR command,
	 * rather it reads all messages and searches for a match.
	 * 
	 * @param gatewayId
	 *            The Gateway ID of the gateway to read from.
	 * @param memLoc
	 *            The memory location string.
	 * @param memIndex
	 *            The memory index.
	 * @return The message read. Null if no relevant message is found or if the
	 *         Gateway ID given is invalid.
	 * @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 InboundMessage readMessage(String gatewayId, String memLoc, int memIndex) throws TimeoutException, GatewayException, IOException, InterruptedException
	{
		if (getServiceStatus() == ServiceStatus.STOPPED) return null;
		InboundMessage msg = null;
		AGateway gateway = findGateway(gatewayId);
		if ((gateway != null) && (gateway.isInbound()))
		{
			try
			{
				msg = gateway.readMessage(memLoc, memIndex);
			}
			catch (TimeoutException e)
			{
				getLogger().logWarn("readMessages(): Gateway " + gateway.getGatewayId() + " does not respond, marking for restart.", null, null);
				gateway.setStatus(GatewayStatuses.RESTART);
			}
			catch (IOException e)
			{
				getLogger().logWarn("readMessages(): Gateway " + gateway.getGatewayId() + " throws IO errors, marking for restart.", null, null);
				gateway.setStatus(GatewayStatuses.RESTART);
			}
		}
		return msg;
	}

	/**
	 * Sends a single message. The following logic is applied in order for
	 * SMSLib to decide from which gateway it will send the message:<br>
	 * 1. If the message holds gateway information (member field "gatewayId"),
	 * SMSLib will try to send it from that gateway.<br>
	 * 2. If the message does not hold gateway information (member field
	 * "gatewayId" is empty or "*") then if router and load balancer is defined,
	 * then message is processed by these classes.<br>
	 * 3. Otherwise the method selects the first outbound-capable gateway
	 * defined and sends the message from it.<br>
	 * The method blocks until the message is actually sent (synchronous
	 * operation).
	 * 
	 * @param msg
	 *            An OutboundMessage object.
	 * @return True if the message is 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 #queueMessage(OutboundMessage)
	 */
	public boolean sendMessage(OutboundMessage msg) throws TimeoutException, GatewayException, IOException, InterruptedException
	{
		if (getServiceStatus() == ServiceStatus.STOPPED) return false;
		AGateway gateway = routeMessage(msg);
		if (gateway != null)
		{
			try
			{
				List<String> recipients = expandGroup(msg.getRecipient());
				if (recipients.size() == 0) return gateway.sendMessage(msg);
				else
				{
					List<OutboundMessage> groupMessages = new ArrayList<OutboundMessage>();
					for (String to : recipients)
					{
						OutboundMessage newMessage = new OutboundMessage();
						msg.copyTo(newMessage);
						newMessage.setRecipient(to);
						groupMessages.add(newMessage);
					}
					sendMessages(groupMessages);
					return true;
				}
			}
			catch (TimeoutException e)
			{
				getLogger().logWarn("sendMessage(): Gateway " + gateway.getGatewayId() + " does not respond, marking for restart.", null, null);
				gateway.setStatus(GatewayStatuses.RESTART);
				msg.setMessageStatus(MessageStatuses.FAILED);
				msg.setFailureCause(FailureCauses.GATEWAY_FAILURE);
				return false;
			}
			catch (IOException e)
			{
				getLogger().logWarn("sendMessage(): Gateway " + gateway.getGatewayId() + " throws IO errors, marking for restart.", null, null);
				gateway.setStatus(GatewayStatuses.RESTART);
				msg.setMessageStatus(MessageStatuses.FAILED);
				msg.setFailureCause(FailureCauses.GATEWAY_FAILURE);
				return false;
			}
		}
		else return false;
	}

	/**
	 * Sends a single message from the specified gateway.
	 * 
	 * @param msg
	 *            An OutboundMessage object.
	 * @param gatewayId
	 *            The id of the gateway that will be used for sending.
	 * @return True if the message is 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 boolean sendMessage(OutboundMessage msg, String gatewayId) throws TimeoutException, GatewayException, IOException, InterruptedException
	{
		if (getServiceStatus() == ServiceStatus.STOPPED) return false;
		msg.setGatewayId(gatewayId);
		return sendMessage(msg);
	}

	/**
	 * Sends a list of messages.
	 * 
	 * @param msgList
	 *            A list of OutboundMessage objects.
	 * @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(Collection<OutboundMessage> msgList) throws TimeoutException, GatewayException, IOException, InterruptedException
	{
		if (getServiceStatus() == ServiceStatus.STOPPED) return 0;
		int counter = 0;
		for (OutboundMessage msg : msgList)
			if (sendMessage(msg)) counter++;
		return counter;
	}

	/**
	 * .NET bridge method.
	 */
	public int sendMessages(OutboundMessage[] msgArray) throws TimeoutException, GatewayException, IOException, InterruptedException
	{
		int counter = 0;
		for (int i = 0; i < msgArray.length; i++)
			if (sendMessage(msgArray[i])) counter++;
		return counter;
	}

	/**
	 * Sends a list of messages from the specified gateway.
	 * 
	 * @param msgList
	 *            A list of OutboundMessage objects.
	 * @param gatewayId
	 *            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(Collection<OutboundMessage> msgList, String gatewayId) throws TimeoutException, GatewayException, IOException, InterruptedException
	{
		if (getServiceStatus() == ServiceStatus.STOPPED) return 0;
		int counter = 0;
		for (OutboundMessage msg : msgList)
		{
			msg.setGatewayId(gatewayId);
			if (sendMessage(msg)) counter++;
		}
		return counter;
	}

	/**
	 * .NET bridge method.
	 */
	public int sendMessages(OutboundMessage[] msgArray, String gatewayId) throws TimeoutException, GatewayException, IOException, InterruptedException
	{
		int counter = 0;
		for (int i = 0; i < msgArray.length; i++)
		{
			msgArray[i].setGatewayId(gatewayId);
			if (sendMessage(msgArray[i])) 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)
	{
		if (getServiceStatus() == ServiceStatus.STOPPED) return false;
		List<String> recipients = expandGroup(msg.getRecipient());
		if (recipients.size() == 0) return getQueueManager().queueMessage(msg);
		else
		{
			for (String to : recipients)
			{
				OutboundMessage newMessage = new OutboundMessage();
				msg.copyTo(newMessage);
				newMessage.setRecipient(to);
				getQueueManager().queueMessage(msg);
			}
			return true;
		}
	}

	/**
	 * Queues a message for sending from the specific gateway.
	 * 
	 * @param msg
	 *            A OutboundMessage object.
	 * @param gatewayId
	 *            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 gatewayId)
	{
		if (getServiceStatus() == ServiceStatus.STOPPED) return false;
		msg.setGatewayId(gatewayId);
		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(Collection<OutboundMessage> msgList)
	{
		int counter = 0;
		if (getServiceStatus() == ServiceStatus.STOPPED) return 0;
		for (OutboundMessage msg : msgList)
			if (queueMessage(msg)) counter++;
		return counter;
	}

	/**
	 * .NET bridge method.
	 */
	public int queueMessages(OutboundMessage[] msgArray)
	{
		int counter = 0;
		if (getServiceStatus() == ServiceStatus.STOPPED) return 0;
		for (int i = 0; i < msgArray.length; i ++)
			if (queueMessage(msgArray[i])) counter++;
		return counter;
	}

	/**
	 * Queues a list of messages for sending from the specific gateway.
	 * 
	 * @param msgList
	 *            A list of OutboundMessage objects.
	 * @param gatewayId
	 *            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(Collection<OutboundMessage> msgList, String gatewayId)
	{
		if (getServiceStatus() == ServiceStatus.STOPPED) return 0;
		int counter = 0;
		for (OutboundMessage msg : msgList)
		{
			msg.setGatewayId(gatewayId);
			if (queueMessage(msg)) counter++;
		}
		return counter;
	}

	/**
	 * .NET bridge method.
	 */
	public int queueMessages(OutboundMessage[] msgArray, String gatewayId)
	{
		int counter = 0;
		if (getServiceStatus() == ServiceStatus.STOPPED) return 0;
		for (int i = 0; i < msgArray.length; i ++)
		{
			msgArray[i].setGatewayId(gatewayId);
			if (queueMessage(msgArray[i])) counter++;
		}
		return counter;
	}

	/**
	 * Attempts to remove the specified message from the background sending
	 * queue. Can only be used for messages previously queued up with the
	 * queueMessage() calls.
	 * 
	 * @param msg
	 *            The outbound message to be removed.
	 * @return true if the message is indeed removed from the respective
	 *         background queue.
	 */
	public boolean removeMessage(OutboundMessage msg)
	{
		if (getServiceStatus() == ServiceStatus.STOPPED) return false;
		return getQueueManager().removeMessage(msg);
	}

	/**
	 * 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
	{
		if (getServiceStatus() == ServiceStatus.STOPPED) return false;
		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.", null, null);
				gateway.setStatus(GatewayStatuses.RESTART);
				return false;
			}
			catch (IOException e)
			{
				getLogger().logWarn("deleteMessage(): Gateway " + gateway.getGatewayId() + " throws IO errors, marking for restart.", null, null);
				gateway.setStatus(GatewayStatuses.RESTART);
				return false;
			}
		}
		return false;
	}

	/**
	 * Loads the phonebook from the specified gateway into a Phonebook class.
	 * 
	 * @param phonebook
	 *            An already instantiated, empty Phonebook class.
	 * @param gatewayId
	 *            The gateway id for which the phonebook should be loaded
	 * @return The number of phonebook entries read.
	 * @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 int readPhonebook(Phonebook phonebook, String gatewayId) throws TimeoutException, GatewayException, IOException, InterruptedException
	{
		if (getServiceStatus() == ServiceStatus.STOPPED) return 0;
		AGateway gateway = findGateway(gatewayId);
		if (gateway != null) { return gateway.readPhonebook(phonebook); }
		return 0;
	}

	/**
	 * 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.
	 */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -