0

I'm using Grizzly instead of Glassfish for this JAX-RS application. I'm new to this technology so I've been googling around a lot and can't seem to find a good outline for how to setup a Service/DAO layer in my applicatiion.

Below is the working prototype that I have.

My Resource

@Path("/helloworld")
@Stateless
public class MyResource {

    @EJB //DOESN'T WORK - how do I map this service to this resource?
    WorkflowService workflowService;

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String TestRequest() {
        Workflow workflow = new Workflow();
        workflow.setName("test");

        workflowService.save(workflow);

        return "Workflow ID:";
    }
}

My Dao

public class WorkflowDao {

    @PersistenceContext(unitName = "unit")
    private EntityManager entityManager;

    public int save(Workflow workflow) {
        entityManager.persist(workflow);
        return workflow.getId();
    }
}

My Service

@Stateless
public class WorkflowService {

    @EJB //I know EJB is probably wrong here, not sure what else to do yet.
    WorkflowDao workflowDao;

    public int save(Workflow workflow) {
        int id = workflowDao.save(workflow);
        return id;
    }
}

Update - I realize EJB won't work with my setup. So my question is, what does? How do I make the service accessible in my resource?

-------------- Final/Working Code --------------

Resource

@Path("/helloworld")
public class MyResource {

    WorkflowService workflowService;

    public MyResource() {
        workflowService = new WorkflowService();
    }

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String TestRequest() {
        Workflow workflow = new Workflow();
        workflow.setName("test");

        workflowService.save(workflow);

        return "Workflow ID:";
    }
}

Service

public class WorkflowService {

    WorkflowDao workflowDao;

    public WorkflowService() {
        workflowDao = new WorkflowDao();
    }

    public int save(Workflow workflow) {
        int id = workflowDao.save(workflow);
        return id;
    }
}

DAO

@Singleton
public class WorkflowDao {

    private EntityManager entityManager;

    public int save(Workflow workflow) {
        getEntityManager().persist(workflow);
        return workflow.getId();
    }

    protected EntityManager getEntityManager() {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("unit");
        EntityManager em = emf.createEntityManager();
        return em;
    }
}
Ben
  • 60,438
  • 111
  • 314
  • 488

1 Answers1

1

I'm not sure I can answer your question fully. But I can tell you that @EJBis part of JavaEE 6 which can only be run in a WebApplication Server.

Grizzly is not one of them (compared to Glassfish which is one).

EDIT

If you want to have dependency injection you can use the Spring framework.

  • To answer your updated question, I've added Spring in my previous answer. Also, you can choose to instantiate your services in your JAXRS implementation, and make your DAO a singleton. –  Feb 14 '13 at 15:08
  • When they're instantiated in my JAXRS implement can you provide a code example? For instance, I know I can do `workflowservice = new WorkflowService();`, but how will I map the `EntityManager` to the DAO? I'll add `@Singleton` to my DAO – Ben Feb 14 '13 at 15:13
  • You can just use the singleton pattern to make your DAO a singleton ;) see http://en.wikipedia.org/wiki/Singleton_pattern#Eager_initialization –  Feb 14 '13 at 15:16
  • Can you provide any insight on how I should map the `EntityManager`? – Ben Feb 14 '13 at 15:41
  • 1
    You can use an `EntityManagerFactory`, although I cannot remember the exact syntax for using it. –  Feb 14 '13 at 15:44