Java/EJB3/Web Services — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
 
м (1 версия)
 
(нет различий)

Текущая версия на 06:50, 1 июня 2010

EJB Based Web Services

File: jndi.properties
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost:1099

File: Main.java
import java.util.Date;
import javax.naming.InitialContext;
import bean.EmployeeServiceRemote;

public class Main {
  public static void main(String[] a) throws Exception {
    EmployeeServiceRemote service = null;
    // Context compEnv = (Context) new InitialContext().lookup("java:comp/env");
    // service = (HelloService)new InitialContext().lookup("java:comp/env/ejb/HelloService");
    service = (EmployeeServiceRemote) new InitialContext().lookup("EmployeeBean/remote");
    
    

    
    //service.doAction();
  }
}

File: Employee.java
package bean;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.PostRemove;

@Entity
public class Employee implements java.io.Serializable {
  private int id;
  private String firstName;
  private String lastName;
  @Id
  @GeneratedValue
  public int getId() {
    return id;
  }

  @PostRemove
  public void postRemove()
  {
     System.out.println("@PostRemove");
  }
  public void setId(int id) {
    this.id = id;
  }
  public String getFirstName() {
    return firstName;
  }
  public void setFirstName(String first) {
    this.firstName = first;
  }
  public String getLastName() {
    return lastName;
  }
  public void setLastName(String last) {
    this.lastName = last;
  }
}

File: EmployeeBean.java
package bean;
import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@WebService(name = "MyEmployee", serviceName = "MyEmployeeService")
//@Stateless
public class EmployeeBean implements EmployeeServiceLocal, EmployeeServiceRemote {
  @PersistenceContext(unitName = "EmployeeService")
  private EntityManager entityManager;
  public EmployeeBean() {
  }
  @WebMethod
  public void createEmployee(@WebParam(name = "employee")Employee c) {
    entityManager.persist(c);
  }
  @WebMethod
  @WebResult(name = "Employee")
  public Employee findEmployee(@WebParam(name = "ID")int pKey) {
    return entityManager.find(Employee.class, pKey);
  }
}

File: EmployeeServiceLocal.java
package bean;

import javax.ejb.Local;

@Local
public interface EmployeeServiceLocal {
  public void createEmployee(Employee c);
  public Employee findEmployee(int id);
}

File: EmployeeServiceRemote.java
package bean;
import javax.ejb.Remote;

@Remote
public interface EmployeeServiceRemote {
  public void createEmployee(Employee c);
  public Employee findEmployee(int id);
}





EJB Tutorial from JBoss: turn EJB to web service

File: Calculator.java
/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2006, Red Hat Middleware LLC, and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.tutorial.webservice.bean;
import javax.jws.WebService;
import javax.jws.WebMethod;
import java.rmi.Remote;
import java.rmi.RemoteException;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
@WebService
@SOAPBinding(style=Style.RPC)
public interface Calculator extends Remote
{
   @WebMethod int add(int x, int y);
   @WebMethod int subtract(int x, int y);
}

File: CalculatorBean.java
/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2006, Red Hat Middleware LLC, and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.tutorial.webservice.bean;
import javax.ejb.Stateless;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
@Stateless
@WebService(endpointInterface="org.jboss.tutorial.webservice.bean.Calculator")
public class CalculatorBean
{
   public int add(int x, int y)
   {
      return x + y;
   }
   public int subtract(int x, int y)
   {
      return x - y;
   }
}
File: Client.java
/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2006, Red Hat Middleware LLC, and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.tutorial.webservice.client;
import org.jboss.tutorial.webservice.bean.Calculator;
import javax.xml.namespace.QName;
import javax.xml.rpc.Service;
import javax.xml.rpc.ServiceFactory;
import java.net.URL;
import java.io.File;
public class Client
{
   public static void main(String[] args) throws Exception
   {
      URL url = new URL("http://localhost:8080/tutorial/CalculatorBean?wsdl");
      QName qname = new QName("http://bean.webservice.tutorial.jboss.org/jaws",
              "CalculatorService");
      ServiceFactory factory = ServiceFactory.newInstance();
      Service service = factory.createService(url, qname);
      Calculator calculator = (Calculator) service.getPort(Calculator.class);
      System.out.println("1 + 1 = " + calculator.add(1, 1));
      System.out.println("1 - 1 = " + calculator.subtract(1, 1));
   }
}





EJB With Web Method

File: jndi.properties
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost:1099

File: Main.java
import javax.naming.InitialContext;
import bean.EmployeeServiceRemote;
public class Main {
  public static void main(String[] a) throws Exception {
    EmployeeServiceRemote service = null;
    // Context compEnv = (Context) new InitialContext().lookup("java:comp/env");
    // service = (HelloService)new
    // InitialContext().lookup("java:comp/env/ejb/HelloService");
    service = (EmployeeServiceRemote) new InitialContext().lookup("EmployeeBean/remote");
    service.doAction();
  }
}

File: Employee.java
package bean;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.PostRemove;
@Entity
public class Employee implements java.io.Serializable {
  private int id;
  private String firstName;
  private String lastName;
  @Id
  @GeneratedValue
  public int getId() {
    return id;
  }

  @PostRemove
  public void postRemove()
  {
     System.out.println("@PostRemove");
  }
  public void setId(int id) {
    this.id = id;
  }
  public String getFirstName() {
    return firstName;
  }
  public void setFirstName(String first) {
    this.firstName = first;
  }
  public String getLastName() {
    return lastName;
  }
  public void setLastName(String last) {
    this.lastName = last;
  }
}

File: EmployeeBean.java
package bean;
import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebService;
@Stateless(name = "EmployeeBeanEJB")
@WebService(serviceName = "EmployeeBeanWebService", 
            targetNamespace = "http://www.jexp.ru/ejb3/credit")
public class EmployeeBean implements EmployeeServiceLocal, EmployeeServiceRemote {
  public EmployeeBean() {
  }
  @WebMethod(operationName = "CreditCheck")
  public boolean validateCC(String cc) {
    return true;
  }
  public void doAction() {
    System.out.println("Processing...");
  }
}

File: EmployeeServiceLocal.java
package bean;
import javax.ejb.Local;
import javax.ejb.Remote;

@Local
public interface EmployeeServiceLocal{
  public void doAction();
}

File: EmployeeServiceRemote.java
package bean;
import javax.ejb.Stateless;
import javax.jws.WebService;

public interface EmployeeServiceRemote {
  public void doAction();

}





Turn Ejb To Web Service

File: jndi.properties
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost:1099

File: Main.java
import javax.naming.InitialContext;
import bean.CountRemote;
public class Main {
  public static void main(String[] a) throws Exception {
    String name = "jexp";
    CountRemote service = null;
    // Context compEnv = (Context) new InitialContext().lookup("java:comp/env");
    // service = (HelloService)new
    // InitialContext().lookup("java:comp/env/ejb/HelloService");
    service = (CountRemote) new InitialContext().lookup("CountBean/remote");
    int countVal = 9;
    service.set(countVal);
    countVal = service.count();
    System.out.println(countVal);
    System.out.println("Calling count() on beans...");
    countVal = service.count();
    System.out.println(countVal);
    service.remove();
  }
}
File: CountBean.java
package bean;
import javax.ejb.Remote;
import javax.ejb.Remove;
import javax.ejb.Stateless;
import javax.jws.WebService;
@Stateless
@Remote(CountRemote.class)
@WebService(serviceName="Counter", portName="CounterPort")
public class CountBean implements CountLocal, CountRemote {
    private int val;
    public int count() {
        System.out.println("count()");
        return ++val;
    }
    public void set(int val) {
        this.val = val;
        System.out.println("set()");
    }
    @Remove
    public void remove() {
        System.out.println("remove()");
    }
}

File: CountLocal.java
package bean;

import javax.ejb.Local;
@Local
public interface CountLocal  {
    /**
     * Increments the counter by 1
     */
    public int count();
    /**
     * Sets the counter to val
     * @param val
     */
    public void set(int val);
    /**
     * removes the counter
     */
    public void remove();
  }

File: CountRemote.java
package bean;

import javax.ejb.Remote;
@Remote
public interface CountRemote{
  /**
   * Increments the counter by 1
   */
  public int count();
  /**
   * Sets the counter to val
   * @param val
   */
  public void set(int val);
  /**
   * removes the counter
   */
  public void remove();
}





Web Method With Return Type And Parameters

File: EmployeeBean.java

import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
@Stateless
@WebService(serviceName = "EmployeeBeanService", targetNamespace = "urn:EmployeeBean")
@SOAPBinding(style = SOAPBinding.Style.DOCUMENT)
public class EmployeeBean implements EmployeeServiceLocal, EmployeeServiceRemote {
  public EmployeeBean() {
  }
  @WebMethod
  @WebResult(name = "bidNumber")
  public Long addBid(@WebParam(name = "User")String userId, 
      @WebParam(name = "Item")Long itemId, 
      @WebParam(name = "Price")Double bidPrice) {
    System.out.println("Bid for " + itemId + " received with price" + bidPrice);
    return 0L;
  }
}

File: EmployeeServiceLocal.java

import javax.ejb.Local;
import javax.jws.WebParam;
@Local
public interface EmployeeServiceLocal {
  public Long addBid(String userId,Long itemId,Double bidPrice);

}

File: EmployeeServiceRemote.java
import javax.ejb.Remote;

@Remote
public interface EmployeeServiceRemote {
  public Long addBid(String userId,Long itemId,Double bidPrice);
}

File: jndi.properties
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost:1099

File: Employee.java

import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.PostRemove;
@Entity
public class Employee implements java.io.Serializable {
  private int id;
  private String firstName;
  private String lastName;
  @Id
  @GeneratedValue
  public int getId() {
    return id;
  }

  @PostRemove
  public void postRemove()
  {
     System.out.println("@PostRemove");
  }
  public void setId(int id) {
    this.id = id;
  }
  public String getFirstName() {
    return firstName;
  }
  public void setFirstName(String first) {
    this.firstName = first;
  }
  public String getLastName() {
    return lastName;
  }
  public void setLastName(String last) {
    this.lastName = last;
  }
}

File: Main.java
import javax.ejb.EJB;
import javax.naming.InitialContext;

public class Main {
  public static void main(String[] a) throws Exception {
    EmployeeServiceRemote service = null;
    // Context compEnv = (Context) new InitialContext().lookup("java:comp/env");
    // service = (HelloService)new
    // InitialContext().lookup("java:comp/env/ejb/HelloService");
    service = (EmployeeServiceRemote) new InitialContext().lookup("EmployeeBean/remote");

    //service.doAction();
    

  }
}