在项目中调用其他系统的WebService发那边发送数据,但是有状态更新时他们坑爹的不推送。只能在这边用线程去查询,在发送成功之后将要查询的数据放入线程查询表。然后通过线程查询放入到队列,在用线程去跑队列里面的数据。按时查询通过状态比对有状态更新时,修改状态,写入日志表。到最终状态时修改线程查询表状态,使线程查询时不再遍历此数据。
构建的队列:
package com.hwt.glmf.util;
import java.util.LinkedList;
/**
* 隊列管理器
* @author lenovo
*
*/
@SuppressWarnings("unchecked")
public class QueueList {
public static LinkedList list = new LinkedList();
public static void clear() {// 销毁队列
list.clear();
}
public static boolean queueEmpty() {// 判断队列是否为空
return list.isEmpty();
}
public static void enQueue(Object o) {// 进队
list.addLast(o);
}
public static Object deQueue() {// 出队
if (!list.isEmpty()) {
return list.removeFirst();
}
return null;
}
public static int queueLength() {// 获取队列长度
return list.size();
}
public static Object queuePeek() {// 查看队首元素
return list.getFirst();
}
}
查询线程表的线程:
package com.hwt.glmf.util;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import com.hwt.glmf.enterpriseIfno.service.EnterpriseIfnoService;
import com.hwt.glmf.enterpriseIfno.vo.SendDeclareRptinfo;
/**
* 回执队列数据
*
* @author lenovo
*
*/
@SuppressWarnings(value="all")
public class ReadDbRptThread extends Thread {
private static final Logger logger = Logger.getLogger(ReadDbRptThread.class);
public static QueueList queue = new QueueList();
private EnterpriseIfnoService enterpriseIfnoService = (EnterpriseIfnoService) SpringContextHelper.getBean("enterpriseIfnoService");// 队列对象
/** 是否退出线程标识 */
private boolean isExit;
private static ReadDbRptThread instance;
public static ReadDbRptThread getInstance() {
if(ReadDbRptThread.instance == null){
ReadDbRptThread.instance = new ReadDbRptThread();
}
return ReadDbRptThread.instance;
}
/**
* 线程执行方法
*/
public void run() {
while(!isExit){
try {
if(queue.queueEmpty()){//处理完了在读
SendDeclareRptinfo rpt = new SendDeclareRptinfo();
rpt.setOverpro("0");//申报流程未结束的才处理,否则不处理
List<SendDeclareRptinfo> list =
enterpriseIfnoService.querySendDeclareRptinfoList(rpt);
for (SendDeclareRptinfo sendDeclareRptinfo : list) {
queue.enQueue(sendDeclareRptinfo);
}
}
//logger.info("线程运行完毕,休眠");
sleep(1000*60*10);//十分钟执行一次
} catch (Exception e) {
try {
sleep(1000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}
}
}
/**
* 退出线程
*/
public void exit() {
synchronized (this) {
this.isExit = true;
}
if (logger.isDebugEnabled()) {
logger.debug("Thread[ReadDbRptThread] exit.");
}
}
}
查询状态的线程:
package com.hwt.glmf.util;
import org.apache.log4j.Logger;
import com.hwt.glmf.billInfo.webservice.CiqWebServiceClient;
import com.hwt.glmf.enterpriseIfno.vo.SendDeclareRptinfo;
import com.hwt.glmf.mfOpShip.query.mfOsQueryUtil;
import com.hwt.glmf.qp.webservice.QpWebServiceClient;
/**
* 自动申报线程
*
* @author lenovo
*
*/
@SuppressWarnings(value="all")
public class ReadWebServiceThread extends Thread {
private static final Logger logger = Logger.getLogger(ReadWebServiceThread.class);
private static final String SEAM_FLAG = "SEAM_FLAG";//海运舱单
private static final String QP_FLAG = "QP_FLAG";//QP,报关单
private static final String CIQ_FLAG = "CIQ_FLAG";//国检
/** 是否退出线程标识 */
private boolean isExit;
private static ReadWebServiceThread instance;
public static ReadWebServiceThread getInstance() {
if(ReadWebServiceThread.instance == null){
ReadWebServiceThread.instance = new ReadWebServiceThread();
}
return ReadWebServiceThread.instance;
}
/**
* 线程执行方法
*/
public void run() {
while(!isExit){
try {
while (!ReadDbRptThread.queue.queueEmpty()) {
SendDeclareRptinfo rpt = (SendDeclareRptinfo) ReadDbRptThread.queue.deQueue();
if(rpt != null){
/**
* 各自写各自的回执查询接口
* 方法内容包括:
* 1.请求对应的回执接口
* 2.回执回来后更新到对应的业务表里面
* 3.如果该业务已经完成最终的状态,则需要更新队列回执表SEND_DECLARE_RPTINFO中OVERPRO值为1
* 此时今后将不再更新回执
*/
String end = rpt.getOverpro();
boolean flag = end.equals("0");
if(SEAM_FLAG.equals(rpt.getType()) && flag){//海运舱单
//doXXX(rpt);
mfOsQueryUtil.showMarshaller(rpt);
}
if(QP_FLAG.equals(rpt.getType()) && flag){//QP,报关单
QpWebServiceClient.qpRptBussiness(rpt);
}
if(CIQ_FLAG.equals(rpt.getType()) && flag){//国检
//doXXX(rpt);
CiqWebServiceClient.ciqRptBussiness(rpt);
}
}
}
//logger.info("线程运行完毕,休眠");
sleep(1000*60*8);//十分钟执行一次
} catch (Exception e) {
e.printStackTrace();
try {
sleep(1000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
}
/**
* 退出线程
*/
public void exit() {
synchronized (this) {
this.isExit = true;
}
if (logger.isDebugEnabled()) {
logger.debug("Thread[FileWatchThread] exit.");
}
}
}