有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

spring mvc Java,线程不继续使用Hibernate

对不起,我的英语不好

我的程序有一个大问题,我会用SpringMVC创建一个数据生成器模拟器。这是代码:

类别执行人:

public class ExecutorHerga {
public ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);

public void startExecutor(int freq, int idsensore) {
    //HergaRunnable.getInstance(idsensore);
    scheduledExecutorService.scheduleAtFixedRate(HergaRunnable.getInstance(idsensore) , 0, freq, TimeUnit.SECONDS);
 }
}

类线程可运行:

public class HergaRunnable implements Runnable {

public Random random = new Random();
public static double min = 0;
public static double max = 100;
private static HergaRunnable hergaRunnable = null;
private static Acquisizione acquisizione;



public static HergaRunnable getInstance(int id_sensore) {
    if (hergaRunnable == null) {
        hergaRunnable = new HergaRunnable();
        acquisizione = new Acquisizione();
        acquisizione.setId_sensore(id_sensore);
        return hergaRunnable;
    }
    return hergaRunnable;
}

@Override
public void run() {
    double number = min + (max - min) * random.nextDouble();
    min = number;
    Calendar c = Calendar.getInstance();
    System.out
            .println("Numero :" + number + " alla data : " + c.getTime() + " " + Thread.currentThread().getName());
    acquisizione.setValore_acquisizione(number);
    acquisizione.setCommenti("ok");
    AcquisizioneDao acquisizioneDao = new AcquisizioneDaoImpl();
    acquisizioneDao.generaAcquisizioni(acquisizione);

}

这是控制休眠的类:

@Repository("acquisizioneDao")
public class AcquisizioneDaoImpl extends AbstractDao<Integer, Acquisizione> implements AcquisizioneDao {

private static final Logger logger = LoggerFactory.getLogger(UserDaoImpl.class);

@Autowired
SensoriService sensoriService;

public static AcquisizioneDaoImpl acquisizioneDaoImpl=null;


@SuppressWarnings("unchecked")
public List<Acquisizione> getAcquisizioni(int id_sensore) {
    logger.info(" getAcquisizioni per il sensore : "+id_sensore);
    Criteria crit = createEntityCriteria();
    crit.add(Restrictions.eq("id_sensore", id_sensore));
    List<Acquisizione> acquisizione = (List<Acquisizione>) crit.list();
    for(int i=0; i<acquisizione.size();i++) {
        logger.info("valore : "+i+ acquisizione.get(i).toString());
    }

    return acquisizione;
}


public void generaAcquisizioni(Acquisizione acquisizione) {
    persist(acquisizione);
}

这就是电话:

public void pressionHerga(Sensore sensore, int numberOfRead) {
    logger.info("pressionHerga");

    ExecutorHerga ex = new  ExecutorHerga();
    ex.startExecutor(numberOfRead,sensore.getId_sensore());
}

现在,如果我可以开始,线程循环一次并停止:

    10:15:08.926 [http-bio-8080-exec-9] INFO  c.w.springmvc.dao.UserDaoImpl - Ricerca nome sel sensore
10:15:08.926 [http-bio-8080-exec-9] INFO  c.w.springmvc.dao.UserDaoImpl - La ricerca ha dato il seguente nominativo :Herga-6101-0001 
10:15:08.926 [http-bio-8080-exec-9] INFO  c.w.springmvc.dao.UserDaoImpl - switch riconosciuto
10:15:08.926 [http-bio-8080-exec-9] INFO  c.w.springmvc.dao.UserDaoImpl - pressionHerga
Numero :23.142418038941848 alla data : Thu Sep 06 10:15:08 CEST 2018 pool-1-thread-1
10:15:08.942 [http-bio-8080-exec-9] INFO  c.w.springmvc.dao.UserDaoImpl - findById Sensore
Hibernate: 
    select
        sensore0_.id_sensore as id_senso1_5_0_,
        sensore0_.descrizione as descrizi2_5_0_,
        sensore0_.nome_sensore as nome_sen3_5_0_,
        sensore0_.unita_misura as unita_mi4_5_0_,
        sensore0_.valore_limite as valore_l5_5_0_,
        sensore0_.valore_massimo as valore_m6_5_0_,
        sensore0_.valore_minimo as valore_m7_5_0_,
        sensore0_.valore_range as valore_r8_5_0_ 
    from
        sensore sensore0_ 
    where
        sensore0_.id_sensore=?
10:15:08.961 [http-bio-8080-exec-9] INFO  c.w.springmvc.dao.UserDaoImpl -  getAcquisizioni per il sensore : 3
Hibernate: 
    select
        this_.id_acquisizione as id_acqui1_4_0_,
        this_.commenti as commenti2_4_0_,
        this_.id_sensore as id_senso3_4_0_,
        this_.valore_acquisizione as valore_a4_4_0_ 
    from
        acquisizione this_ 
    where
        this_.id_sensore=?

如果我对代码进行注释

“//acquisizioneDao.generaacquisioni(acquisizione);”

存在于类HergaRunnable的方法run()中

输出为:

    Numero :37.27787955579307 alla data : Thu Sep 06 10:36:41 CEST 2018 pool-1-thread-1
10:36:41.494 [http-bio-8080-exec-10] INFO  c.w.springmvc.dao.UserDaoImpl - findById Sensore
Hibernate: 
    select
        sensore0_.id_sensore as id_senso1_5_0_,
        sensore0_.descrizione as descrizi2_5_0_,
        sensore0_.nome_sensore as nome_sen3_5_0_,
        sensore0_.unita_misura as unita_mi4_5_0_,
        sensore0_.valore_limite as valore_l5_5_0_,
        sensore0_.valore_massimo as valore_m6_5_0_,
        sensore0_.valore_minimo as valore_m7_5_0_,
        sensore0_.valore_range as valore_r8_5_0_ 
    from
        sensore sensore0_ 
    where
        sensore0_.id_sensore=?
10:36:41.504 [http-bio-8080-exec-10] INFO  c.w.springmvc.dao.UserDaoImpl -  getAcquisizioni per il sensore : 3
Hibernate: 
    select
        this_.id_acquisizione as id_acqui1_4_0_,
        this_.commenti as commenti2_4_0_,
        this_.id_sensore as id_senso3_4_0_,
        this_.valore_acquisizione as valore_a4_4_0_ 
    from
        acquisizione this_ 
    where
        this_.id_sensore=?
10:36:41.512 [http-bio-8080-exec-10] INFO  c.w.s.d.HibernateTokenRepositoryImpl - getPrincipal
Numero :82.95177323858331 alla data : Thu Sep 06 10:36:43 CEST 2018 pool-1-thread-1
Numero :89.2202317734951 alla data : Thu Sep 06 10:36:45 CEST 2018 pool-1-thread-1
Numero :93.21668730833589 alla data : Thu Sep 06 10:36:47 CEST 2018 pool-1-thread-1
Numero :94.08899028381322 alla data : Thu Sep 06 10:36:49 CEST 2018 pool-1-thread-1

我什么都试过了,但我想不出怎么解决,你能帮我吗


共 (0) 个答案