我有一个在下面的代码中使用的 SQL 查询。
从 STATUS_CHECK 中选择 STATUS,其中 uniqueuId = ?
STATUS 的不同值是 NOT STARTED
、RUNNING
、COMPLETE
、ERROR
。
我需要不时检查 STATUS 的值,并在发现 COMPLETE
后立即执行一些操作,并想知道应该遵循什么方法来完成我的任务。我可以朝哪个方向前进有什么想法吗?
现在,在下面的代码中,我只看到 NOT STARTED
并且我的代码退出。而且我不知道如何检查状态,而不需要在一段时间后再次调用 Web 服务,以确保它更改为 COMPLETE
以便我可以对其进行一些操作。
假设我有以下 Controller 、DAO 和 DAoImple 来获取STATUS
Controller :
@RequestMapping(value = "/statusCheck", method = RequestMethod.POST)
public void statusCheck
(
@RequestParam(value = "id", defaultValue = "") String id
) {
try {
StatusDao sDao = (StatusDao) context.getBean("sDao");
sDao.statusLookUp(id);
} catch (Throwable th) {th.printStackTrace();}
}
数据访问对象(DAO):
public interface StatusDao {
void statusLookUp(String id) throws DaoException;
}
DAO 实现:
public class StatusDaoImpl implements StatusDao {
public void setDataSource(DataSource dataSource) {
jdbcTemplate = new JdbcTemplate(dataSource);
}
@Override
public void statusLookUp(String id) throws DaoException {
DataSource ds = null;
Connection conn = null;
PreparedStatement pstmt = null;
CallableStatement cStmt = null;
ResultSet rs = null;
try {
ds = jdbcTemplate.getDataSource();
conn = ds.getConnection();
cStmt = conn.prepareCall("{call STATUS_RETRIEVER.GET_STATUS(?)}");
cStmt.setString(1, id);
cStmt.registerOutParameter(2, Types.VARCHAR);
int uniqueId = cStmt.getInt(2);
pstmt = conn.prepareStatement("SELECT STATUS FROM STATUS_CHECK WHERE uniqueuId=?");
pstmt.setInt(1, uniqueId);
rs = pstmt.executeQuery();
rs.next();
String status = rs.getString("STATUS");
System.out.println("The status received is as follows:");
System.out.println(status);
} catch (Throwable th) {
throw new DaoException(th.getMessage(), th);
}
}
}
最佳答案
您可以根据需要尝试安排任务每秒运行一次,甚至以毫秒为单位运行。 J2EE和Spring框架都提供作业调度功能。 您可以以固定时间延迟安排任务,或使用 Cron 表达式安排任务。 检查这里。
关于java - 如何在不调用多个 Ajax Web 服务的情况下继续查询表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54793315/