0

IDE : NetBeans 8.1
AP Server: GlassFish 4.1
Framework: JSF2.2 + Omnifaces 2.2

When I right-click on the Project > Run as Pic 1

the applicationBean with @Eager annotation(Omnifaces) was started before the web application deployed(from server.log), and it makes some exception or bug:

  1. This web container has not yet been started
  2. Object: model.entity.NePingMin[ id=null ] is not a known entity type.

BUT at Services > Server > right-click on GlassFish > Start as Pic 2

It works without any exception...... lol

It is caused by Omnifaces bugs?

Application Bean:

import org.omnifaces.cdi.Eager;
import javax.inject.Named;
import javax.enterprise.context.ApplicationScoped;

@Named
@ApplicationScoped
@Eager
public class pingController  {

    PingThread pingThread;

    @PostConstruct
    public void init() {
        t = new PingThread();
        t.start();
        ....

Other Code:

public class PingThread extends Thread {

@Override
public void run() {

    while (true) {
        try {
            Thread.sleep(30000);
            spmsEm = JPAUnit.getSPMSEntityManagerFactory().createEntityManager(); //RESOURCE_LOCAL

            //PingResult is just a simple POJO
            //but it will throw java.lang.IllegalStateException:
            // This web container has not yet been started
            PingResult result = new PingResult()

        ....

        try {
            EntityTransaction eT = spmsEm.getTransaction();
            if (!eT.isActive()) {
                eT.begin();
             }

            //An entity class
            //It will throw java.lang.IllegalArgumentException:
            // Object: model.entity.NePingMin[ id=null ] is not a known entity type.
            NePingMin np = new NePingMin(); 
            np.setCount(p.count);
            np.setNeId(c.getControllerId());
            np.setNeType("controller");
            np.setSuccessCount(p.successCount);
            np.setRecordTime(new Date(System.currentTimeMillis()));
            spmsEm.persist(np);
            eT.commit();
        } catch (Exception ex) {
                ex.printStackTrace();
        }
        .....
Gmoz
  • 21
  • 8
  • I'd say it's a bug in Netbeans itself. That it did something different during build/deploy which caused a timing fail. As to the problem you're trying to solve with `@Eager`, the code looks much like it fits better in a `@Startup @Singleton` EJB with `@Schedule`. In an EJB you also don't need to manually fiddle around with transactions and threads this way. See also a.o. http://stackoverflow.com/q/6149919 and http://stackoverflow.com/q/18369356 – BalusC Apr 14 '16 at 10:16
  • Thanks! It's helpful, I will try it. – Gmoz Apr 15 '16 at 00:55
  • If I want to run multiple thread instances(is like PingThread in post) in a @Startup@Singleton EJB, which annotation should I use at these threads? And is it possible to use/inject JTA entitymanger or I must manually create resource_local entitymanger? thank you :) – Gmoz Apr 15 '16 at 08:26

0 Answers0