0

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?

Mike
  • 2,393
  • 3
  • 25
  • 37

1 Answers1

0

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;
 }
}
uaarkoti
  • 3,659
  • 2
  • 20
  • 23
  • That gets me a reference to an interface to a proxy that links up to the session bean, from the war side. That's great if the war needs to pull data from the session bean. But the session bean needs to PUSH data OUT to an object inside the war. I don't think I can just pass a reference to the object inside the war to the session bean can I? The object would get serialized would it not? How to solve the pushing of data FROM the session bean TO the war...? – Mike Mar 22 '12 at 04:28
  • I am not sure I am following what you mean by "PUSH data OUT". Stateful bean simply stores data. When the bean is created, you can store it in the session and refer it anytime you want to access it. If you want event listeners, then you will have to architect accordingly so that changes in the Stateful bean will generate events that other components can register and listen for. – uaarkoti Mar 22 '12 at 21:20
  • I didn't mention anything about a stateful or stateless bean. I mentioned a session bean. I should have mentioned that there is business logic in the session bean. This thus isn't about storing data, but for it to perform business logic that some outside entity can invoke. The business logic in the session bean then needs to "push results out" to an object inside the war component. – Mike Mar 26 '12 at 15:59