web service, contexts and sessions
oswalidoss Apr 16, 2010 1:44 PMhello,
I'm a newbie, now i'm trying to implement a web service,
Environnement:
jboss 4.2
seam 2.0
EJB 3
jpa
eclipse
jbosstools
I followed this : http://docs.jboss.org/seam/2.1.0.GA/reference/en-US/html/webservices.html
I have two entities, a session class to do the work for the web service and a web service class,
all that in well configured seam project.
Job.java :
`package entity;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Name;
import com.sun.xml.bind.CycleRecoverable;
@SuppressWarnings("serial") //i'm ignoring this warning cuz i don't know what it's for !
@Entity
@Table(name = "job")
@Name("job")
@AutoCreate
public class Job implements Serializable,CycleRecoverable{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id_job;
private String label;
@OneToMany(fetch = FetchType.LAZY,mappedBy = "job", cascade = CascadeType.ALL)
private List<User> listUser = new ArrayList<User>();
/*
* @see com.sun.xml.bind.CycleRecoverable#onCycleDetected(com.sun.xml.bind.CycleRecoverable.Context)
* this method is invoked when a cycle is detected , exactly while generating xml file to send
* response to client
*/
public Object onCycleDetected(Context arg0) {
Job job = new Job();
job = new Job(this);
job.setListUser(null);
return (Object) job;
}
/*
* I intentionally don't initialize list in the constructor
*/
//Constructor
public Job(Integer id, String label) {
this.id_job = id;
this.label = label;
}
//Default constructor
public Job() {
}
//Copy constructor
public Job(Job job) {
this.id_job = job.getId_job();
this.label = job.getLabel();
}
//Getters and setters
public Integer getId_job() {
return id_job;
}
public void setId_job(Integer id) {
this.id_job = id;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public List<User> getListUser() {
return listUser;
}
public void setListUser(List<User> listUser) {
this.listUser = listUser;
}
}`
User.java :
`package entity;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Name;
import com.sun.xml.bind.CycleRecoverable;
@SuppressWarnings("serial")
@Entity
@Table(name = "user")
@Name("user")
@AutoCreate
public class User implements Serializable,CycleRecoverable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String username;
private String password;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "id_job", nullable = true)
private Job job;
/*
* Constructors
* I intentionally don't copy Job in the Copy constructor cuz i'm using it
* in onCycledetected to stop cycle
*/
public User() {
}
public User(Integer id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
public User(User user) {
setId(user.id);
setPassword(user.getPassword());
setUsername(user.getUsername());
}
/*
* (non-Javadoc)
* @see *com.sun.xml.bind.CycleRecoverable#onCycleDetected(com.sun.xml.bind.CycleRecoverable.Context)
*/
public Object onCycleDetected(Context arg0) {
User user = new User(this);
user.setJob(this.job);
user.getJob().setListUser(this.job.getListUser());
for (User us : this.job.getListUser()){
us.setJob(null);
}
return user;
}
//Getters and setters
public Job getJob() {
return job;
}
public void setJob(Job job) {
this.job = job;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
`
WebServiceSession.java:
`package org.domain.registration.session;
import javax.persistence.EntityManager;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Begin;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import entity.User;
@Name("webservicesession")
@Scope(ScopeType.CONVERSATION)
@AutoCreate
public class WebServiceSession {
@In EntityManager entityManager;
/*
* i enjoyed this exception for a couple of hours : could not initialize proxy - no Session
* when i add @begin it works.
* what i want to know is : how it works without @begin when i use JSF ?
*/
@Begin
public User getUser(){
String query = "select u from User u where u.id = :id";
User user = (User) entityManager.createQuery(query).setParameter("id", 3).getSingleResult();
return user;
}
/*
* what's happen if there is no @End, when the session will be closed and who's going to close it
*/
}
`
ServiceWebTest.java :
`package webServices;
import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebService;
import org.domain.registration.session.WebServiceSession;
import org.jboss.seam.Component;
import org.jboss.wsf.spi.annotation.WebContext;
import entity.User;
@WebService(name = "gpao", serviceName = "gpaoService")
@WebContext(contextRoot = "gpaoContext")
@Stateless
public class ServiceWebTest {
/*
* Since i can't use injection here (i would love to know why and what's the difference),
* i use Component.getInstance to get the session
*/
private WebServiceSession webservicesession =
(WebServiceSession) Component.getInstance("webservicesession");
@WebMethod
public User extraire(){
return webservicesession.getUser();
}
}
`
Everything works as i want but i don't really understand what's going on inside, fortunately when i tried more than
one client i'm obliged to go deeply inside the framework.
Two clients i use are (soapUI plugin for eclipse and anothe client running on netbeans).
I have used eclipse debugger trying to understand what's going on inside, unfortunately
i don't reach a minimal of an understanding level that allow me to continue.
My Questions :
1)every client has a session (conversation) ?
2)when client's thread will be killed ?
3)do the instance of the session class is shared between clients
even some links are welcome .
Best Regards,
walid
I'm a newbie, now i'm trying to implement a web service,
Environnement:
jboss 4.2
seam 2.0
EJB 3
jpa
eclipse
jbosstools
I followed this : http://docs.jboss.org/seam/2.1.0.GA/reference/en-US/html/webservices.html
I have two entities, a session class to do the work for the web service and a web service class,
all that in well configured seam project.
Job.java :
`package entity;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Name;
import com.sun.xml.bind.CycleRecoverable;
@SuppressWarnings("serial") //i'm ignoring this warning cuz i don't know what it's for !
@Entity
@Table(name = "job")
@Name("job")
@AutoCreate
public class Job implements Serializable,CycleRecoverable{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id_job;
private String label;
@OneToMany(fetch = FetchType.LAZY,mappedBy = "job", cascade = CascadeType.ALL)
private List<User> listUser = new ArrayList<User>();
/*
* @see com.sun.xml.bind.CycleRecoverable#onCycleDetected(com.sun.xml.bind.CycleRecoverable.Context)
* this method is invoked when a cycle is detected , exactly while generating xml file to send
* response to client
*/
public Object onCycleDetected(Context arg0) {
Job job = new Job();
job = new Job(this);
job.setListUser(null);
return (Object) job;
}
/*
* I intentionally don't initialize list in the constructor
*/
//Constructor
public Job(Integer id, String label) {
this.id_job = id;
this.label = label;
}
//Default constructor
public Job() {
}
//Copy constructor
public Job(Job job) {
this.id_job = job.getId_job();
this.label = job.getLabel();
}
//Getters and setters
public Integer getId_job() {
return id_job;
}
public void setId_job(Integer id) {
this.id_job = id;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public List<User> getListUser() {
return listUser;
}
public void setListUser(List<User> listUser) {
this.listUser = listUser;
}
}`
User.java :
`package entity;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Name;
import com.sun.xml.bind.CycleRecoverable;
@SuppressWarnings("serial")
@Entity
@Table(name = "user")
@Name("user")
@AutoCreate
public class User implements Serializable,CycleRecoverable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String username;
private String password;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "id_job", nullable = true)
private Job job;
/*
* Constructors
* I intentionally don't copy Job in the Copy constructor cuz i'm using it
* in onCycledetected to stop cycle
*/
public User() {
}
public User(Integer id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
public User(User user) {
setId(user.id);
setPassword(user.getPassword());
setUsername(user.getUsername());
}
/*
* (non-Javadoc)
* @see *com.sun.xml.bind.CycleRecoverable#onCycleDetected(com.sun.xml.bind.CycleRecoverable.Context)
*/
public Object onCycleDetected(Context arg0) {
User user = new User(this);
user.setJob(this.job);
user.getJob().setListUser(this.job.getListUser());
for (User us : this.job.getListUser()){
us.setJob(null);
}
return user;
}
//Getters and setters
public Job getJob() {
return job;
}
public void setJob(Job job) {
this.job = job;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
`
WebServiceSession.java:
`package org.domain.registration.session;
import javax.persistence.EntityManager;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Begin;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import entity.User;
@Name("webservicesession")
@Scope(ScopeType.CONVERSATION)
@AutoCreate
public class WebServiceSession {
@In EntityManager entityManager;
/*
* i enjoyed this exception for a couple of hours : could not initialize proxy - no Session
* when i add @begin it works.
* what i want to know is : how it works without @begin when i use JSF ?
*/
@Begin
public User getUser(){
String query = "select u from User u where u.id = :id";
User user = (User) entityManager.createQuery(query).setParameter("id", 3).getSingleResult();
return user;
}
/*
* what's happen if there is no @End, when the session will be closed and who's going to close it
*/
}
`
ServiceWebTest.java :
`package webServices;
import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebService;
import org.domain.registration.session.WebServiceSession;
import org.jboss.seam.Component;
import org.jboss.wsf.spi.annotation.WebContext;
import entity.User;
@WebService(name = "gpao", serviceName = "gpaoService")
@WebContext(contextRoot = "gpaoContext")
@Stateless
public class ServiceWebTest {
/*
* Since i can't use injection here (i would love to know why and what's the difference),
* i use Component.getInstance to get the session
*/
private WebServiceSession webservicesession =
(WebServiceSession) Component.getInstance("webservicesession");
@WebMethod
public User extraire(){
return webservicesession.getUser();
}
}
`
Everything works as i want but i don't really understand what's going on inside, fortunately when i tried more than
one client i'm obliged to go deeply inside the framework.
Two clients i use are (soapUI plugin for eclipse and anothe client running on netbeans).
I have used eclipse debugger trying to understand what's going on inside, unfortunately
i don't reach a minimal of an understanding level that allow me to continue.
My Questions :
1)every client has a session (conversation) ?
2)when client's thread will be killed ?
3)do the instance of the session class is shared between clients
even some links are welcome .
Best Regards,
walid