Search in sources :

Example 1 with LocalEJBDirectory

use of org.jboss.as.test.clustering.ejb.LocalEJBDirectory in project wildfly by wildfly.

the class StatefulServlet method doGet.

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String module = getRequiredParameter(req, MODULE);
    String bean = getRequiredParameter(req, BEAN);
    HttpSession session = req.getSession(true);
    Incrementor incrementor = (Incrementor) session.getAttribute(BEAN);
    if (incrementor == null) {
        try (LocalEJBDirectory directory = new LocalEJBDirectory(module)) {
            incrementor = directory.lookupStateful(bean, Incrementor.class);
        } catch (NamingException e) {
            throw new ServletException(e);
        }
    }
    try {
        resp.setHeader(COUNT, String.valueOf(incrementor.increment()));
        session.setAttribute(BEAN, incrementor);
    } catch (NoSuchEJBException e) {
        resp.setHeader(COUNT, String.valueOf(0));
        session.removeAttribute(BEAN);
    }
    resp.getWriter().write("Success");
}
Also used : ServletException(javax.servlet.ServletException) NoSuchEJBException(javax.ejb.NoSuchEJBException) HttpSession(javax.servlet.http.HttpSession) Incrementor(org.jboss.as.test.clustering.cluster.ejb.stateful.bean.Incrementor) NamingException(javax.naming.NamingException) LocalEJBDirectory(org.jboss.as.test.clustering.ejb.LocalEJBDirectory)

Example 2 with LocalEJBDirectory

use of org.jboss.as.test.clustering.ejb.LocalEJBDirectory in project wildfly by wildfly.

the class StatefulServlet method doGet.

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    HttpSession session = req.getSession(true);
    Stateful bean = (Stateful) session.getAttribute(BEAN);
    if (bean == null) {
        try (LocalEJBDirectory directory = new LocalEJBDirectory(StatefulBean.MODULE)) {
            bean = directory.lookupStateful(StatefulBean.class, Stateful.class);
        } catch (NamingException e) {
            throw new ServletException(e);
        }
    }
    Command command = Command.valueOf(req.getParameter(COMMAND));
    log.trace(StatefulServlet.class.getName() + ": command = " + command);
    String answer = null;
    switch(command) {
        case CREATE_EMPLOYEE:
            {
                bean.createEmployee("Tom Brady", "New England Patriots", 1);
                answer = bean.getEmployee(1).getName();
                break;
            }
        case GET_EMPLOYEE:
            {
                Employee employee = bean.getEmployee(1);
                if (employee == null) {
                    throw new ServletException("couldn't load Employee entity (with id=1) from database");
                }
                answer = employee.getName();
                break;
            }
        case DELETE_EMPLOYEE:
            {
                bean.deleteEmployee(1);
                answer = command.toString();
                break;
            }
        case GET_EMPLOYEES_IN_2LC:
            {
                long count = bean.getEmployeesInMemory();
                if (count == -1) {
                    throw new ServletException("couldn't get number of employees in second level cache");
                }
                answer = String.valueOf(count);
                break;
            }
        case GET_2ND_BEAN_EMPLOYEE:
            {
                Employee employee = bean.getSecondBeanEmployee(1);
                answer = employee.getName();
                break;
            }
        case DESTROY:
            {
                bean.destroy();
                answer = command.toString();
                break;
            }
        case FLUSH:
            {
                bean.flush();
                break;
            }
        case ECHO:
            {
                bean.echo(req.getParameter(MESSAGE));
                break;
            }
        case CLEAR:
            {
                bean.clear();
                break;
            }
        case DELETE_EMPLOYEE_DIRECT:
            {
                // delete all employees in db
                int deleted = bean.executeNativeSQL("delete from Employee where id=1");
                if (deleted < 1) {
                    throw new ServletException("couldn't delete entity in database, deleted row count =" + deleted);
                }
                break;
            }
    }
    if (answer != null) {
        resp.setHeader(ANSWER, answer);
    }
    resp.getWriter().write("Success");
    session.setAttribute(BEAN, bean);
    log.trace(StatefulServlet.class.getName() + ": command = " + command + " finished");
}
Also used : Stateful(org.jboss.as.test.clustering.cluster.ejb.xpc.bean.Stateful) ServletException(javax.servlet.ServletException) Employee(org.jboss.as.test.clustering.cluster.ejb.xpc.bean.Employee) HttpSession(javax.servlet.http.HttpSession) StatefulBean(org.jboss.as.test.clustering.cluster.ejb.xpc.bean.StatefulBean) NamingException(javax.naming.NamingException) LocalEJBDirectory(org.jboss.as.test.clustering.ejb.LocalEJBDirectory)

Aggregations

NamingException (javax.naming.NamingException)2 ServletException (javax.servlet.ServletException)2 HttpSession (javax.servlet.http.HttpSession)2 LocalEJBDirectory (org.jboss.as.test.clustering.ejb.LocalEJBDirectory)2 NoSuchEJBException (javax.ejb.NoSuchEJBException)1 Incrementor (org.jboss.as.test.clustering.cluster.ejb.stateful.bean.Incrementor)1 Employee (org.jboss.as.test.clustering.cluster.ejb.xpc.bean.Employee)1 Stateful (org.jboss.as.test.clustering.cluster.ejb.xpc.bean.Stateful)1 StatefulBean (org.jboss.as.test.clustering.cluster.ejb.xpc.bean.StatefulBean)1