gpt4 book ai didi

java - 在实例中运行并在再次运行前停止 5 分钟的代码

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:14:09 25 4
gpt4 key购买 nike

我想在实例中为每个实例运行我的代码代码从我的数据库中提取数据作为结果集并停止等待另外 5 分钟提取结果集并停止。该程序应该永远不会结束。问题是程序在第一个实例中等待 5 分钟,但再次等待它继续运行下面是代码。结果集和数据库连接部分没问题

package postilion_error.checker;

/**
*
* @author kithinjid
*/
import java.io.IOException;
import java.sql.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Timer;
import java.util.logging.*;
import java.util.TimerTask;
import static sun.font.FontManager.logger;

public class POSTILION_ERRORCHECKER {


static Logger log;
static Handler file_handler;
Connection conn=null;
Statement st=null;
static ResultSet rs=null;
static int i = 0;
static int counter=0;
static boolean checker=true;


private void dbconnect()
{
String username="Postilion";
String password="Password12";

String hostname="jdbc:sqlserver://172.16.112.6;databaseName=realtime";
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn=DriverManager.getConnection(hostname,username,password);
System.out.println("TIME:"+dateString("MM:dd-HH:mm:ss")+":connected to database");
}
catch(Exception e)
{
e.printStackTrace();
}
}
private ResultSet check_00(String query1)
{
dbconnect();

try
{
st=conn.createStatement();
rs=st.executeQuery(query1);
}catch(Exception e)
{
e.printStackTrace();
}


return rs ;
}
public static void scheduled()
{


Timer timer=new Timer();
timer.schedule(new TimerTask() {

@Override
public void run() {
while(checker==true)
{
log=logger.getLogger("my logger");
try {
file_handler=new FileHandler("C:\\Users\\kithinjid\\Google Drive\\interswitch\\NetBeansProjects\\POSTILION_ERROR CHECKER\\dist\\logg.txt");
SimpleFormatter formatter=new SimpleFormatter();
file_handler.setFormatter(formatter);
log.addHandler(file_handler);
} catch (IOException ex) {
log.info(dateString("HHmmss"));
Logger.getLogger(POSTILION_ERRORCHECKER.class.getName()).log(Level.SEVERE, null, ex);
} catch (SecurityException ex)
{
Logger.getLogger(POSTILION_ERRORCHECKER.class.getName()).log(Level.SEVERE, null, ex);
}
String query="SELECT TOP 100 [tran_nr]\n" +
" ,[gmt_date_time]\n" +
" ,[time_local]\n" +
" ,[date_local]\n" +
" ,[sink_node]\n" +
" ,[rsp_code_req_rsp]\n" +
" ,[rsp_code_cmp]\n" +
" ,[rsp_code_rev]\n" +
" FROM [realtime].[dbo].[tm_trans_10] where sink_node = 'GTBUSDsnk' ORDER BY tran_nr desc";
POSTILION_ERRORCHECKER pe=new POSTILION_ERRORCHECKER();
pe.dbconnect();


try
{
ResultSet result=pe.check_00(query);

while(result.next()==true)
{
//loop for counter and displaying
int response=Integer.parseInt(result.getString("rsp_code_req_rsp"));
System.out.println(response);
if (response==91)
{
counter++;
if (counter>=10)
System.out.println("TIME:"+dateString("MM:dd-HH:mm:ss")+"--error respose code:91 of count:"+ counter+" to sink node GTBUSDsnk");
}

}
}catch(Exception e)
{
e.printStackTrace();
}
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
},30000,30000);

}

public static void main(String[] args)
{
scheduled();
System.out.println("execution started");
}
private static String dateString(String formart)
{
DateFormat dateFormat = new SimpleDateFormat(formart);
java.util.Date date = new java.util.Date();
return dateFormat.format(date);
}


}

最佳答案

您可以如下创建一个名为 delay() 的函数,然后在您想要等待 5 分钟时调用该函数。但这可能会阻塞你的程序,而且如果你同时需要做其他事情,它也不会工作......

private void delay()
{
try {
Thread.sleep(5 * 60 * 1000);
} catch(Exception e) { }
}

关于java - 在实例中运行并在再次运行前停止 5 分钟的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43683674/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com