<bean id="transferInJobProcessor" class="cn.namemax.job.transferin.TransferInJobProcessor"
p:defaultRegistrarId="1" />
public class CdnMessageProcessor implements Runnable {
private static final Log log = LogFactory.getLog(MessageProcessor.class);
private Thread workerThread;
private boolean running = true;
@Override
public void run() {
log.info("Starting cdnMessageProcessor...");
while (running) {
try {
Thread.sleep(60000L);
try {
Session session = SessionFactoryUtils.getSession(sessionFactory, true);
TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
执行方法体;
}
} catch (Exception e) {
log.error(e, e);
} finally {
SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.unbindResource(sessionFactory);
SessionFactoryUtils.closeSession(sessionHolder.getSession());
}
} catch (InterruptedException e) {
}
}
log.info("MessageProcessor stopped...");
}
//构造bean后执行的方法,执行后台目标方法(run)
@PostConstruct
public void start() {
workerThread = new Thread(this, "CNNIC_CDN_MessageProcessor");
workerThread.start();
}
@PreDestroy
public void stop() {
log.info("Stopping MessageProcessor...");
running = false;
workerThread.interrupt();
}
}