3 Replies Latest reply on Apr 17, 2010 10:57 AM by oswalidoss

    web service, contexts and sessions

    oswalidoss
      hello,
      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
        • 1. Re: web service, contexts and sessions
          oswalidoss

          Sorry for the formatting, i can't find how to edit what i wrote

          • 2. Re: web service, contexts and sessions
            gaborj

            Hi,



            1. Yes, the session represents the state of the client on the server side. Conversations are segments of session.

            2. After some time of inactivity a session times out, resources get released. You can set timeout in config and should not care resources in most cases.

            3. No, as Session represents state, it is not shared.





            Do you like reading? You should.


            First some Java EE, Servlet, probably JSP and all EE stuff you like...


            The number of documents on Java EE is countless, I'm sure you will find good sources of information, and do not want to make marketing here... some kind of home is : SUN Java EE pages where you find the specs...


            Then you should go for Seam Docs, these books are really good, worth reading!


            G.

            • 3. Re: web service, contexts and sessions
              oswalidoss

              Hello,


              think you so much for the replay , yes i love reading , i'll focus on concepts and details as soon as possible,
              can you give some books (in french if possible) names so i can save the time of surfing and looking for the best docs ?


              Best Regards,
              walid