jfinal afterJFinalStart中执行长久循环操作的解决方案:创建新线程

很多时候,需要在jfinal中afterJFinalStart方法中,写一些需要一直循环运行的程序,做一些循环操作。但是在afterJFinalStart中,执行时间过长的话,会导致整个站点启动超时。

解决方案是,新建一个新的线程,在afterJFinalStart中启动即可:

 1 package com.thread;
 2 
 3 import com.activeMQ.ActiveMQHelper;
 4 import com.demo.testSpring.Animal;
 5 
 6 /**
 7  * 新创建线程,用于接受消息,并对应操作
 8  * @author czg
 9  *
10  */
11 public class worker extends Thread{
12     
13     public void run() {
14         String msg;
15         ActiveMQHelper activeMQHelper=new ActiveMQHelper("tcp://localhost:61616?wireFormat.maxInactivityDuration=0",2000);
16 
17         while(true){
18             msg=activeMQHelper.receiveMessage("hello");
19             if(!msg.equals("EOF")) System.out.println(msg);
20         
21         }
22     }
23 }
线程实现
1 @Override
2     public void afterJFinalStart(){
3         
4         Thread workerThread = new Thread(new com.thread.worker());
5         workerThread.start();
6     }
线程启动

 

posted @ 2017-05-04 11:40  向_日_葵  阅读(1383)  评论(0编辑  收藏  举报