Stateful JAX-WS with CDI – Part 1

Usually it is desirable to keep web services stateless. This is because it allows for easier scalability (since no state has to be shared across multiple instances) and reduced load on system resources (as no state has to be held or cleaned up).

However, there are scenarios where it may be desirable to maintain server sided states across multiple requests. As I have demonstrated in my previous post CDI CONVERSATIONS FOR JAX-RS WITH JEE 6, it is possible to utilize CDI conversation scope in JAX-RS restful services. CDI conversation scope was designed to deal with the limitation of having one session per browser, allowing for tab-specific state to be kept on the server side.

JAX-WS is primarily used in fat-/smart-client server scenarios. Fat-/Smart-clients, unlike browsers, are capable of managing multiple sessions as required. JAX-WS has build-in support for SOAP and HTTP session management (see JAX-WS 2.2 Rev a, Chp. 10.4.1.4 Session Management).

Server-Side
Setting up the session context on the server side is a simple matter of defining a @SessionScoped CDI bean…

[java]
@SessionScoped
public class Interactions implements Serializable {

private List<String> names = new ArrayList<>();

public String interact(String name) {
final String interactions = "Hi " + name + ", I previously interacted with " + names;
names.add(name);
return interactions;
}

public Integer getNumberOfInteractions() {
return names.size();
}
}
[/java]
Continue reading Stateful JAX-WS with CDI – Part 1