0

Initial Problem

Hi,

I use a @WebListener Class to initiate a RMI connection at application deployment. That connects my JSF frontend with a backend.

That works fine!

Next I want to hand the connection to a ManagedBean because I want to use the connection to e.g. save something out of a bean, since weblistener is not accessible out of xhtml pages.

I tried to put a managedProperty into that class but I think that's not allowed. So how to do that?

@WebListener
public class Config implements ServletContextListener {

public static final String SERVER_NAMING = "xxx";
public static final String SERVER_HOST = "xxx"; 

public static FrontendCommInterface server;


public void contextInitialized(ServletContextEvent event) {
    try {

        server = (FrontendCommInterface) Naming.lookup("rmi://" + SERVER_HOST + "/" + SERVER_NAMING); 
            System.out.println("Connection successfull!");
//HERE THE SERVER SHOULD HANDED TO ANOTHER MANAGEDBEAN !!! BUT HOW TO DO THAT??? 

        } catch (MalformedURLException e) {
        System.out.print("Error: " + e.getLocalizedMessage());
    } catch (RemoteException e) {
        System.out.print("Error: " + e.getLocalizedMessage());
    } catch (NotBoundException e) {
        System.out.print("Error: " + e.getLocalizedMessage());
    }
}

public void contextDestroyed(ServletContextEvent event) {
    // Do stuff during webapp's shutdown.
} 
Community
  • 1
  • 1
Sven
  • 6,288
  • 24
  • 74
  • 116

1 Answers1

2

You need to create the bean and put in application scope yourself.

event.getServletContext().setAttribute("communication", new Communication(server));
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I can use ManagedProperties in WebListners? – Sven Jan 09 '11 at 12:17
  • No, definitely not. Note that I updated the answer since I misunderstood the initial problem due to lack of coffee this early morning ;) – BalusC Jan 09 '11 at 12:23
  • No problem :-D. But in the meantime I tried to add your other advice that included postConstruct and it works :-) Thank you – Sven Jan 09 '11 at 12:34