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

📄 smssvr.java

📁 Sending and receiving of SMS using Java
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
					srv.getLogger().info(">>> QUEUE TO: " + msg.getGatewayId() + " : " + msg.getRecipient());
				}
			}
		}
		rs.close();
		cmd.close();
		con.close();
	}

	private void readMessages() throws Exception
	{
		List msgList;

		msgList = new ArrayList();
		srv.readMessages(msgList, MessageClasses.ALL);
		saveToDatabase(msgList);
		if (props.getProperty("settings.delete_after_processing").equalsIgnoreCase("YES"))
		{
			for (int i = 0, n = msgList.size(); i < n; i++)
				srv.deleteMessage((InboundMessage) msgList.get(i));
		}
	}

	private boolean checkTimeFrame(String priority)
	{
		String timeFrame;
		String from, to, current;
		Calendar cal = Calendar.getInstance();

		if (priority.equalsIgnoreCase("L")) timeFrame = props.getProperty("settings.timeframe.low", "0000-2359");
		else if (priority.equalsIgnoreCase("N")) timeFrame = props.getProperty("settings.timeframe.normal", "0000-2359");
		else if (priority.equalsIgnoreCase("H")) timeFrame = props.getProperty("settings.timeframe.high", "0000-2359");
		else timeFrame = "0000-2359";
		from = timeFrame.substring(0, 4);
		to = timeFrame.substring(5, 9);
		cal.setTime(new java.util.Date());
		current = cal.get(Calendar.HOUR_OF_DAY) < 10 ? "0" + cal.get(Calendar.HOUR_OF_DAY) : "" + cal.get(Calendar.HOUR_OF_DAY);
		current += cal.get(Calendar.MINUTE) < 10 ? "0" + cal.get(Calendar.MINUTE) : "" + cal.get(Calendar.MINUTE);
		if ((Integer.parseInt(current) >= Integer.parseInt(from)) && (Integer.parseInt(current) < Integer.parseInt(to))) return true;
		else return false;
	}

	private void execute() throws Exception
	{
		initialize();
		resetQueuedMessages();
		try
		{
			srv.startService();
			while (true)
			{
				srv.getLogger().info("Processing @ " + new java.util.Date());
				try
				{
					srv.getLogger().debug("SMSSvr: before printing statistics.");
					if (!shutdown)
					{
						srv.getLogger().debug("** GATEWAY STATISTICS **");
						for (int i = 0; i < srv.getGatewayList().size(); i ++)
						{
							AGateway gtw = (AGateway) srv.getGatewayList().get(i);
							srv.getLogger().debug("Gateway: " + gtw.getGatewayId() + ", Load: " + srv.getGatewayQueueLoad(gtw.getGatewayId()));
						}
						srv.getLogger().debug("** GATEWAY STATISTICS **");
					}
					srv.getLogger().debug("SMSSvr: before readMessages().");
					if (!shutdown) readMessages();
					srv.getLogger().debug("SMSSvr: after readMessages().");
					srv.getLogger().debug("SMSSvr: before sendMessages().");
					if (!shutdown) sendMessages();
					srv.getLogger().debug("SMSSvr: after sendMessages().");
				}
				catch (Exception e)
				{
					srv.getLogger().error("SMSSvr error!", e);
				}
				if (optRunOnce)
				{
					srv.stopService();
					break;
				}
				else Thread.sleep(Integer.parseInt(props.getProperty("settings.poll_interval", "60")) * 1000);
			}
		}
		catch (Exception e)
		{
			srv.stopService();
			e.printStackTrace();
			srv.getLogger().fatal("Fatal error during Service initialization, aborting...");
		}
	}

	private synchronized void updateOutboundDatabase(OutboundMessage msg) throws Exception
	{
		Connection con = null;
		ResultSet rs = null;
		Statement cmd = null;

		con = getDbConnection();
		if (con == null) return;
		cmd = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
		rs = cmd.executeQuery("select * from " + props.getProperty("database.tables.sms_out", "smssvr_out") + " where id = " + msg.getId());
		if (rs.next())
		{
			if (msg.getMessageStatus() == MessageStatuses.SENT)
			{
				rs.updateString("status", "S");
				rs.updateTimestamp("sent_date", new Timestamp(msg.getDispatchDate().getTime()));
				rs.updateString("gateway_id", msg.getGatewayId());
			}
			else if (msg.getMessageStatus() == MessageStatuses.FAILED)
			{
				int errors = rs.getInt("errors");
				errors++;
				rs.updateInt("errors", errors);
				if (errors > Integer.parseInt(props.getProperty("settings.retries", "2"))) rs.updateString("status", "F");
				else rs.updateString("status", "U");
			}
			if (msg.getRefNo() != null && msg.getRefNo().length() > 0) rs.updateString("ref_no", msg.getRefNo());
			rs.updateRow();
			con.commit();
			rs.close();
			cmd.close();
			con.close();
		}
	}

	private class OutboundNotification implements IOutboundMessageNotification
	{
		public void process(String gatewayId, OutboundMessage msg)
		{
			try
			{
				updateOutboundDatabase(msg);
			}
			catch (Exception e)
			{
				srv.getLogger().fatal(e);
			}
		}
	}

	private class CallNotification implements ICallNotification
	{
		public void process(String gatewayId, String callerId)
		{
			Connection con = null;
			Statement cmd = null;
			ResultSet rs = null;

			try
			{
				con = getDbConnection();
				if (con == null) return;
				cmd = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
				rs = cmd.executeQuery("select * from " + props.getProperty("database.tables.calls", "smssvr_calls") + " where id = -1");
				rs.moveToInsertRow();
				rs.updateTimestamp("call_date", new Timestamp(new java.util.Date().getTime()));
				rs.updateString("gateway_id", gatewayId);
				rs.updateString("caller_id", callerId);
				rs.insertRow();
				con.commit();
				rs.close();
				cmd.close();
				con.close();
			}
			catch (SQLException e)
			{
				e.printStackTrace();
				try
				{
					if (rs != null) rs.close();
					if (cmd != null) cmd.close();
					if (con != null) con.close();
				}
				catch (Exception e1)
				{
					srv.getLogger().fatal(e1);
				}
			}
		}
	}

	private Connection getDbConnection()
	{
		Connection dbCon = null;

		while (!shutdown)
		{
			try
			{
				dbCon = DriverManager.getConnection(props.getProperty("database.url"), props.getProperty("database.username", ""), props.getProperty("database.password", ""));
				dbCon.setAutoCommit(false);
				break;
			}
			catch (SQLException e)
			{
				srv.getLogger().warn("Database lost, trying to get connection back...");
				try { Thread.sleep(5000); } catch (Exception e1) {}
			}
		}
		return dbCon;
	}

	public static void main(String[] args)
	{
		SMSSvr app = new SMSSvr();

		for (int i = 0; i < args.length; i ++)
		{
			if (args[i].equalsIgnoreCase("-runonce")) app.optRunOnce = true;
			else System.out.println("Invalid argument: " + args[i]);
		}

		// This is done just to keep SMSSvr alive in case of fatal errors.
		while (true)
		{
			try
			{
				app.execute();
			}
			catch (FileNotFoundException e)
			{
				System.out.println("Cannot find SMSSvr configuration file!");
				break;
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
			if (app.optRunOnce) break;
		}
	}
}

⌨️ 快捷键说明

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