StartCommand.java 4.28 KB
package com.bsth;


import com.bsth.thread.PersonnelUpdateThread;
import com.bsth.util.DateUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.regex.Pattern;

/**
 * 随应用启动运行
 *
 */
@Component
public class StartCommand implements CommandLineRunner{

	public static ScheduledExecutorService mainServices = Executors.newScheduledThreadPool(2);
	
	Logger log = LoggerFactory.getLogger(this.getClass());
	
	private static long timeDiff;
	
	@Autowired
	PersonnelUpdateThread personnelUpdateThread;

	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	static {
		// 中午12:00
		timeDiff = (DateUtils.getTimestamp() + 1000 * 60 * 60 * 24) - System.currentTimeMillis();
	}

	@Override
	public void run(String... arg0){
		
		try {
			log.info("在:"+timeDiff / 1000 / 60 + "分钟后开始同步数据");
			personnelUpdateThread.run();
			//mainServices.scheduleAtFixedRate(personnelUpdateThread, timeDiff / 1000, 60 * 60 * 24, TimeUnit.SECONDS);//timeDiff / 1000
			//analyzeInterface();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private Pattern pattern = Pattern.compile("");

	private static class InterfaceRequest {

		private String ip;

		private String password;

		private String agent;

		private String uri;

		private int time;

		public InterfaceRequest(String ip, String password, String agent, String uri, int time) {
			this.ip = ip;
			this.password = password;
			this.agent = agent;
			this.uri = uri;
			this.time = time;
		}

		public String getIp() {
			return ip;
		}

		public void setIp(String ip) {
			this.ip = ip;
		}

		public String getPassword() {
			return password;
		}

		public void setPassword(String password) {
			this.password = password;
		}

		public String getAgent() {
			return agent;
		}

		public void setAgent(String agent) {
			this.agent = agent;
		}

		public String getUri() {
			return uri;
		}

		public void setUri(String uri) {
			this.uri = uri;
		}

		public int getTime() {
			return time;
		}

		public void setTime(int time) {
			this.time = time;
		}
	}

	public void analyzeInterface () throws IOException {
		//String line = "INFO -[][180.167.126.126][Apache-HttpClient/4.5.5 (Java/1.8.0_241)][/webservice/rest/schedule_real/execs][{\"password\":[\"e126853c7f6f43b4857fa8dfe3b28b5d90be9e68\"],\"timestamp\":[\"1686192455081\"],\"nonce\":[\"2mycf6\"],\"sign\":[\"d945f2d522db4d458694a435e46a81c5ff9ee73f\"]}]<time:9462>";
		final List<InterfaceRequest> requests = new ArrayList<>();
		String path = "C:\\Users\\Hill\\Downloads\\main1.log";
		BufferedReader reader = new BufferedReader(new FileReader(new File(path)));
		String line = null;
		while ((line = reader.readLine()) != null) {
			int idx = line.indexOf("<time:"), idxPwdStart = line.indexOf("password\":[\""), idxPwdEnd = line.indexOf("\"]", idxPwdStart);
			if (idx > 0) {
				String[] array = line.split("\\]\\[");
				String ip = array[1], agent = array[2], uri = array[3], time = line.substring(idx + 6, line.length() - 1), password = line.substring(idxPwdStart, idxPwdEnd);
				InterfaceRequest request = new InterfaceRequest(ip, password, agent, uri, Integer.parseInt(time));
				requests.add(request);
			}
		}
		jdbcTemplate.batchUpdate("insert into bsth_c_interface_request (ip, password, agent, uri, `time`) values (?, ?, ?, ?, ?)", new BatchPreparedStatementSetter() {
			@Override
			public void setValues(PreparedStatement ps, int i) throws SQLException {
				InterfaceRequest request = requests.get(i);

				ps.setString(1, request.getIp());
				ps.setString(2, request.getPassword());
				ps.setString(3, request.getAgent());
				ps.setString(4, request.getUri());
				ps.setInt(5, request.getTime());
			}

			@Override
			public int getBatchSize() {
				return requests.size();
			}
		});
	}
}