views:

314

answers:

2

Hi all,

I've created simple jax-ws (anotated Java 6 class to web service) service and deploied it on glassfish v3. The web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app>
<servlet>
  <servlet-name>MyServiceName</servlet-name>
  <description>Blablabla</description>
  <servlet-class>com.foo-bar.somepackage.TheService</servlet-class>
  <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>MyServiceName</servlet-name>
  <url-pattern>/MyServiceName</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
</web-app>

There is no sun-jaxws.xml in the war. The service works fine but I have 2 issues: I'm using apache common configuration package to read my configuration, so i have init function that calls configuration stuff. 1. How can I configure init method for jaxws service (like i can do for the servlets for example) 2. the load on startup parameter is not affecting the service, I see that for every request init function called again (and c-tor). How can I set scope for my service?

Thanks a lot,

A: 

How can I configure init method for jaxws service (like i can do for the servlets for example)

JAX-WS endpoints, both web and EJB, can have optional life-cycle methods that are automatically called if present. Any method can be used as a life-cycle method with the correct annotation:

  • @PostConstruct - Called by the container before the implementing class begins responding to web service clients.

  • @PreDestroy - Called by the container before the endpoint is removed from operation

So annotating your init() method with @PostConstruct should do the trick.

the load on startup parameter is not affecting the service, I see that for every request init function called again

Try to use the suggested annotation first. And if you are still facing unexpected behavior, post your code.

Pascal Thivent
A: 

Thanks for the quick answer, Pascal.

BTW, I warmly suggest to use a "valid" servlet 2.5 or servlet 3.0 web.xml (using a version attribute in the web-app element and the xsd declaration).

I'm using 2.5 version, I just didn't paste this part in my post

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:j2ee="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
        http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"&gt;
<description>WebTier for the Login Manager Service</description>
<display-name>LoginManagerWAR</display-name>

<servlet>
    <description>Endpoint for Login Manager Web Service</description>
    <display-name>LoginManagerControllerService</display-name>
    <servlet-name>LoginManagerController</servlet-name>
    <servlet-class>loginmanager.controller.LoginManagerController</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>LoginManagerController</servlet-name>
    <url-pattern>/LoginManagerControllerService</url-pattern>
</servlet-mapping>

<session-config>
    <session-timeout>54</session-timeout>
</session-config>

The PostConstruct works fine , thank you, but load-on-startup still didn't happen.

@WebService(
name="LoginManagerController",
serviceName="LoginManagerControllerService"
)

public class LoginManagerController {   
private ILoginManager manager;

@Resource
private WebServiceContext wsContext;

@PostConstruct
private void init(){
     .....
    }

More over, now every client request makes 2 init() calls of the webservice: like I can see in chainsaw, first called init() of the service, then it called again and then the actually client's function (I print the hash code of the webservice class instance and it the same instance for both calls!!!):

> Message Inside init() method ... controller=31641446

> Message login manager = 11229828

> .....init of elements....blablabla.....

> Message Exiting init() method

> Message Inside init() method ... controller=31641446

> Message login manager = 32361523

The controller is the service and the manager (wich hash code has been changed from first call to the second) created inside the init () of the controller.

I failed to understand what is wrong ....

UPDATE It seems like a to glassfish v3 related issue (maybe my env setup or glassfish configuration). I tried this war on Sailfin and Glassfish V2 and its perfectly working ....

Alex
If you think there is a bug in v3, please complete the circle by filing an issue @ https://glassfish.dev.java.net/servlets/ProjectIssues
vkraemer