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

📄 service.java

📁 基于java开发的一套短信应用框架
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	 *            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 #queueMessages(List, String)
	 */
	public int sendMessages(List msgList, String gtwId) throws TimeoutException, GatewayException, IOException, InterruptedException
	{
		AGateway gateway = findGateway(gtwId);
		if ((gateway != null) && (gateway.isOutbound())) return gateway.sendMessages(msgList);
		else return 0;
	}

	/**
	 * Queue a message for sending by gateway chosen by routing and
	 * loadbalancing mechanism.
	 * 
	 * @param msg
	 *            Message to be sent
	 * @return True if the message is accepted in the Queue.
	 */
	public boolean queueMessage(OutboundMessage msg)
	{
		AGateway gtw = routeMessage(msg);
		if (gtw != null) return gtw.queueMessage(msg);
		else return false;
	}

	/**
	 * Queues a message for sending from the specific gateway. The method does
	 * not block (returns immediately). You may use the gateway's callback
	 * method if you wish to get notified about the outcome of the dispatch.
	 * 
	 * @param msg
	 *            A OutboundMessage object.
	 * @param gtwId
	 *            The id of the gateway that will be used for sending.
	 * @return True if the message is accepted in the Queue.
	 * @see #sendMessage(OutboundMessage)
	 */
	public boolean queueMessage(OutboundMessage msg, String gtwId)
	{
		AGateway gateway = findGateway(gtwId);
		if ((gateway != null) && (gateway.isOutbound())) return gateway.queueMessage(msg);
		else return false;
	}

	/**
	 * Queues a list of messages for sending from the specific gateway. The
	 * method does not block (returns immediately). You may use the gateway's
	 * callback method if you wish to get notified about the outcome of the
	 * dispatch.
	 * 
	 * @param msgList
	 *            A list of OutboundMessage objects.
	 * @param gtwId
	 *            The id of the gateway to be used for sending.
	 * @return The number of messages accepted in the Queue.
	 * @see #sendMessage(OutboundMessage, String)
	 */
	public int queueMessages(List msgList, String gtwId)
	{
		AGateway gateway = findGateway(gtwId);
		if ((gateway != null) && (gateway.isOutbound())) return gateway.queueMessages(msgList);
		else return 0;
	}

	/**
	 * 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
	{
		AGateway gw = findGateway(msg.getGatewayId());
		if (gw != null) return gw.deleteMessage(msg);
		else return false;
	}

	/**
	 * Returns the total number of messages received by the specified gateway.
	 * 
	 * @return The number of received messages.
	 */
	public int getInboundMessageCount(String gtwId)
	{
		AGateway gateway = findGateway(gtwId);
		if (gateway != null) return gateway.getInboundMessageCount();
		else return -1;
	}

	/**
	 * Returns the total number of messages sent via the specified gateway.
	 * 
	 * @return The number of sent messages.
	 */
	public int getOutboundMessageCount(String gtwId)
	{
		AGateway gateway = findGateway(gtwId);
		if (gateway != null) return gateway.getOutboundMessageCount();
		else return -1;
	}

	/**
	 * Returns the total number of messages received by all gateways.
	 * 
	 * @return The number of received messages.
	 */
	public int getInboundMessageCount()
	{
		int total = 0;
		for (int i = 0, n = gtwList.size(); i < n; i++)
			total = total + ((AGateway) gtwList.get(i)).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 (int i = 0, n = gtwList.size(); i < n; i++)
			total = total + ((AGateway) gtwList.get(i)).getOutboundMessageCount();
		return total;
	}

	private void initService()
	{
		gtwList = new ArrayList();
	}

	/**
	 * Find and return a gateway by its ID.
	 * 
	 * @param gtwId
	 *            The ID of gateway to find.
	 * @return Gateway object bearing given name, or NULL if not found.
	 */
	public AGateway findGateway(String gtwId)
	{
		for (int i = 0, n = gtwList.size(); i < n; i++)
			if (((AGateway) gtwList.get(i)).getGatewayId().equals(gtwId)) return (AGateway) gtwList.get(i);
		return null;
	}

	/**
	 * Returns the list of defined gateways.
	 * 
	 * @return The list of gateways.
	 */
	public List getGatewayList()
	{
		return gtwList;
	}

	/**
	 * 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(MessagePriorities)
	 * @see #getGatewayQueueLoad(String)
	 * @see #getGatewayQueueLoad(String, MessagePriorities)
	 */
	public int getGatewayQueueLoad()
	{
		int total = 0;
		for (int i = 0, n = gtwList.size(); i < n; i++)
			total += ((AGateway) gtwList.get(i)).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, MessagePriorities)
	 */
	public int getGatewayQueueLoad(MessagePriorities priority)
	{
		int total = 0;
		for (int i = 0, n = gtwList.size(); i < n; i++)
			total += ((AGateway) gtwList.get(i)).getQueueLoad(priority);
		return total;
	}

	/**
	 * Retrieves the Queue load (i.e. pending messages) from a specific gateway
	 * and for all priorities.
	 * 
	 * @param gtwId
	 *            The Gateway ID for which information is to be retrieved.
	 * @return The number of pending messages to be send.
	 * @see #getGatewayQueueLoad()
	 * @see #getGatewayQueueLoad(MessagePriorities)
	 * @see #getGatewayQueueLoad(String, MessagePriorities)
	 */
	public int getGatewayQueueLoad(String gtwId)
	{
		AGateway gtw = findGateway(gtwId);
		return (gtw == null ? 0 : gtw.getQueueLoad());
	}

	/**
	 * Retrieves the Queue load (i.e. pending messages) from a specific gateway
	 * and for a specific priority.
	 * 
	 * @param gtwId
	 *            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(MessagePriorities)
	 * @see #getGatewayQueueLoad(String)
	 */
	public int getGatewayQueueLoad(String gtwId, MessagePriorities priority)
	{
		AGateway gtw = findGateway(gtwId);
		return (gtw == null ? 0 : gtw.getQueueLoad(priority));
	}

	/**
	 * Returns the Queue delay (i.e. delay between two consecutive message send
	 * attempts by the Queue Manager)
	 * 
	 * @param gtwId
	 *            The Gateway ID for which the delay setting will be returned.
	 * @return The delay in millis.
	 * @see #setGatewayQueueDelay(String, int)
	 */
	public int getGatewayQueueDelay(String gtwId)
	{
		AGateway gtw = findGateway(gtwId);
		return (gtw == null ? -1 : gtw.getQueueDelay());
	}

	/**
	 * Sets the Queue delay (i.e. delay between two consecutive message send
	 * attempts by the Queue Manager)
	 * 
	 * @param gtwId
	 *            The Gateway ID for which the delay will be changed.
	 * @param millis
	 *            The new delay.
	 * @return True if the operation succedded, false if an invalid Gateway Id
	 *         was given.
	 * @see #getGatewayQueueDelay(String)
	 */
	public boolean setGatewayQueueDelay(String gtwId, int millis)
	{
		AGateway gtw = findGateway(gtwId);
		if (gtw != null) gtw.setQueueDelay(millis);
		return (gtw != null);
	}

	/**
	 * Returns the Queue retry count, i.e. the count of times the background
	 * Queue manager will retry to send a failed message before giving up.
	 * 
	 * @param gtwId
	 *            The Gateway ID for which the retries will be returned.
	 * @return The retries.
	 * @see #setGatewayQueueRetries(String, int)
	 */
	public int getGatewayQueueRetries(String gtwId)
	{
		AGateway gtw = findGateway(gtwId);
		return (gtw == null ? -1 : gtw.getQueueDelay());
	}

	/**
	 * Sets the Queue retry count, i.e. the count of times the background Queue
	 * manager will retry to send a failed message before giving up.
	 * 
	 * @param gtwId
	 *            The Gateway ID for which the retries will be returned.
	 * @param retries
	 *            The new retry count.
	 * @return True if the operation succedded, false if an invalid Gateway Id
	 *         was given.
	 * @see #getGatewayQueueRetries(String)
	 */
	public boolean setGatewayQueueRetries(String gtwId, int retries)
	{
		AGateway gtw = findGateway(gtwId);
		if (gtw != null) gtw.setQueueDelay(retries);
		return (gtw != null);
	}

	/**
	 * Returns the active Load Balancer class.
	 * 
	 * @return The active LoadBalancer class.
	 * @see LoadBalancer
	 */
	public LoadBalancer getLoadBalancer()
	{
		return loadBalancer;
	}

	/**
	 * Sets a new Load Balancer.
	 * 
	 * @param loadBalancer
	 *            The Load Balancer that will take effect.
	 * @see LoadBalancer
	 */
	public void setLoadBalancer(LoadBalancer loadBalancer)
	{
		this.loadBalancer = loadBalancer;
	}

	/**
	 * Returns the active Router class.
	 * 
	 * @return The active Router class.
	 * @see Router
	 */
	public Router getRouter()
	{
		return router;
	}

	/**
	 * Sets a new Router.
	 * 
	 * @param router
	 *            The Router that will take effect.
	 * @see Router
	 */
	public void setRouter(Router router)
	{
		this.router = router;
	}

	public void syncGateways() throws TimeoutException, GatewayException, IOException, InterruptedException
	{
		for (int i = 0; i < gtwList.size(); i++)
			((AGateway) gtwList.get(i)).sync();
	}

	/**
	 * 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)
	{
		return router.route(msg);
	}

	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 + -