0
votes

I'm triyng to implement a datasource with Jax-rs server, I already faced of a lot of problems that I resolved by searching, other by deducing, now I found one that I couldn't resolve.

10:31:23,633 INFO [org.jboss.as.ejb3.deployment] (MSC service thread 1-5) WFLYEJB0473: JNDI bindings for session bean named 'NotificacaoDAOEJB' in deployment unit 'deployment "SouVizinho.war"' are as follows:

java:global/SouVizinho/NotificacaoDAOEJB!br.com.br.ejb.NotificacaoDAOEJB java:app/SouVizinho/NotificacaoDAOEJB!br.com.br.ejb.NotificacaoDAOEJB java:module/NotificacaoDAOEJB!br.com.br.ejb.NotificacaoDAOEJB java:global/SouVizinho/NotificacaoDAOEJB java:app/SouVizinho/NotificacaoDAOEJB java:module/NotificacaoDAOEJB

10:31:23,634 INFO [org.jboss.as.ejb3.deployment] (MSC service thread 1-5) WFLYEJB0473: JNDI bindings for session bean named 'NotificacaoControladorEJB' in deployment unit 'deployment "SouVizinho.war"' are as follows:

java:global/SouVizinho/NotificacaoControladorEJB!br.com.souvizinho.controlador.NotificacaoControladorEJB java:app/SouVizinho/NotificacaoControladorEJB!br.com.souvizinho.controlador.NotificacaoControladorEJB java:module/NotificacaoControladorEJB!br.com.souvizinho.controlador.NotificacaoControladorEJB java:global/SouVizinho/NotificacaoControladorEJB java:app/SouVizinho/NotificacaoControladorEJB java:module/NotificacaoControladorEJB

I am requesting "/contador/{id}" by PostMan and when it arrives on notificaoDAO.contador(id) it says the notificacaoDAO is null.

PS: I'm using Wildfly 11 on Eclipse Oxygen

Controller

import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.enterprise.context.RequestScoped;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

@Path("ejb/notificacao")
@Stateless
@LocalBean
public class NotificacaoControladorEJB {


    SmartJava smartjava = new SmartJava();
    @EJB
    NotificacaoDAOEJB notificacaoDAO;


    @GET
    @Produces("application/json; charset=UTF-8")
    @Path("/contador/{id}")
    public Notificacao contadorGet(@PathParam("id") int id) {
        long quantidade;
        try {
            quantidade = notificacaoDAO.contador(id);

            return new Notificacao(quantidade);
        } catch(Exception e) {
            quantidade = 0;
            System.err.println(smartjava.getFullStackTrace(e));

            return new Notificacao(quantidade);
        }
    }

DAO

import java.util.List;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;


@Stateless
@LocalBean
public class NotificacaoDAOEJB {

    //private EntityManagerFactory entityManagerFactory;
    @PersistenceContext
    private EntityManager entityManager;

    SmartJava sj = new SmartJava();



    public Notificacao Salvar(Notificacao notificacao) {

        try {
            this.entityManager.getTransaction().begin();
            this.entityManager.persist(notificacao);
            this.entityManager.getTransaction().commit();
        } catch (Exception e) {
            System.out.println(sj.getFullStackTrace(e));

        } finally {
            //this.entityManager.close();
        }

        return notificacao;
    }

    public void Alterar(Notificacao notificacao){

        this.entityManager.getTransaction().begin();
        this.entityManager.merge(notificacao);
        this.entityManager.getTransaction().commit();
        //this.entityManager.close();

    }

    @SuppressWarnings("unchecked")
    public List<Notificacao> Listar(){
        return this.entityManager.createQuery("SELECT a FROM Notificacao a ORDER BY a.dtcad").getResultList();
    }

    public Notificacao GetNotificacao(int nrseq) {
        return this.entityManager.find(Notificacao.class, nrseq);
    }
    @SuppressWarnings("unchecked")
    public long contador(int nrsequsuario) {
        try {
            return (long) this.entityManager.createQuery("SELECT COUNT(a) FROM Notificacao a "
                    + "WHERE a.visualizado = false AND a.useralvo.nrseq = :usuario ORDER BY a.dtcad")
                    .setParameter("usuario", nrsequsuario).getSingleResult();
        } catch(Exception e) {
            System.err.println(sj.getFullStackTrace(e));
            return 0;
        }
    } 

}
1
Question: Is NotificacaoDAOEJB properly working when injected somewhere else?Al-un
nops, I followed this example: yatb.giacomodrago.com/en/post/7/…Felipe Junges

1 Answers

0
votes

I followed this tutorial:

https://blogs.sap.com/2014/11/26/jax-rs-the-missing-link-between-ui5-and-hcp-java-apps-adding-ejb/

And worked, but I get problem with transaction, so I did the UserTransaction of this topic: A JTA EntityManager cannot use getTransaction()

Now everything works fine, persisting 100ms instead of 5000ms, and listing 50ms instead 3000ms.