Search in sources :

Example 1 with Stateful

use of org.jboss.as.test.clustering.cluster.ejb.xpc.bean.Stateful 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)1 ServletException (javax.servlet.ServletException)1 HttpSession (javax.servlet.http.HttpSession)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 LocalEJBDirectory (org.jboss.as.test.clustering.ejb.LocalEJBDirectory)1