简体   繁体   中英

JBoss session bean pushing data to war app

I have a session bean that is deployed in an ear file controlled by someone else. I'm providing a web-app in the form of either a war file or ear file. I need to provide the ability to that session bean so that it can push data to an object that lives inside my war file. I was thinking of providing an ear file with inside it a war file for the web parts, and a sar file to provide an mbean which can be referenced from that ear file that that other person is managing. I have created mbeans before, but this time the data isn't processed by the mbean, but by an object (singleton accessed?) inside the war app. The war app in essence has to have free reign access to that object that is holding the data.

How do I bridge the gap between the session-bean and the object in the war app?

Any EJB is by default bound to JNDI. You can easily use the JNDI to locate the session bean from within your servlet and execute the session bean. You really don't need to have MBeans.

Here is the pseudo code (just pseudo, may not compile)

A sample stateful bean

package org.jboss.example;
import javax.ejb.Stateful;

@Stateful
public class StatefulBean {

 private String state;

 public String getState() {
     return state;
 }

 public void setState(String state) {
     this.state = state;
 }
}

Sample servlet accessing the above bean

package org.jboss.example;
import java.io.IOException;
import java.io.PrintWriter;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class BeanServlet extends HttpServlet {

 @Override
 protected void doPost(HttpServletRequest request, HttpServletResponse response)
         throws ServletException, IOException {
     PrintWriter writer = response.getWriter();
     StatefulBean statefulBean = getStatefulBean(request);
     writer.println("State: " + statefulBean.getState());
 }

 private StatefulTestBean getStatefulBean(HttpServletRequest request)
         throws ServletException {

     StatefulBean sb = null;
     try {
         InitialContext ic = new InitialContext();
         sb = (StatefulBean) ic.lookup("java:ejb/StatefulBean");

         } catch (NamingException e) {
             throw new ServletException(e);
         }
     }
     return sb;
 }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM