weblogic 9下,把stateless session bean发布为web services

假设我已经把EJB做好了,是一个HelloWorld,下面说如何发布 Bean文件如下:
 
package han.ejb;
 
import java.rmi.RemoteException;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.ejb.CreateException;
/**
 * XDoclet-based session bean. The class must be declared
 * public according to the EJB specification.
 *
 * To generate the EJB related files to this EJB:
 *            - Add Standard EJB module to XDoclet project properties
 *            - Customize XDoclet configuration for your appserver
 *            - Run XDoclet
 * Below are the xdoclet-related tags needed for this EJB.
 * @ejb.bean name="HelloWorldBean"
 *            display-name="HelloWorld"
 *            description="Description for HelloWorld"
 *            jndi-name="ejb/HelloWorld"
 *            remote="HelloWorldRemote"
 *            type="Stateless"
 *            view-type="remote"
 */
public class HelloWorldBean implements SessionBean {
       private static final long serialVersionUID = 1L;
       public HelloWorldBean() {
              // TODO Auto-generated constructor stub
       }
       public void ejbActivate() throws EJBException, RemoteException {
              // TODO Auto-generated method stub
       }
       public void ejbPassivate() throws EJBException, RemoteException {
              // TODO Auto-generated method stub
       }
       public void ejbRemove() throws EJBException, RemoteException {
              // TODO Auto-generated method stub
       }
       /**
        * Set the associated session context. The container calls this method
        * after the instance creation.
        *
        * The enterprise bean instance should store the reference to the context
        * object in an instance variable.
        *
        * This method is called with no transaction context.
        *
        * @throws EJBException Thrown if method fails due to system-level error.
        */
       public void setSessionContext(SessionContext newContext)
              throws EJBException {
       }
       /**
        * An ejbCreate method as required by the EJB specification.
        *
        * The container calls the instance?s <code>ejbCreate</code> method whose
        * signature matches the signature of the <code>create</code> method invoked
        * by the client. The input parameters sent from the client are passed to
        * the <code>ejbCreate</code> method. Each session bean class must have at
        * least one <code>ejbCreate</code> method. The number and signatures
        * of a session bean?s <code>create</code> methods are specific to each
        * session bean class.
        *
        * @throws CreateException Thrown if method fails due to system-level error.
        *
        * @ejb.create-method
        *
        */
       public void ejbCreate() throws CreateException {
              // TODO Add ejbCreate method implementation
       }
       /**
        * An example business method
        *
        * @ejb.interface-method view-type = "remote"
        *
        * @throws EJBException Thrown if method fails due to system-level error.
        */
       @RemoteMethod()
       @WebMethod()
       public String sayhello() throws EJBException {
              // rename and start putting your business logic here
              return new String("Hello EJB!");
       }
}
事实上JSR 181是BEA提出的用于加速Web Services开发的一种基于注释驱动的编程模式,并被批准纳入到J2EE 1.5标准。JSR181提供了一种简单的Web Service开发编程模型和标准的编译及部署方式。只需要编写JSR-175风格的注释就可以制定WSDL,消息产生属性,安全认证方式,以及特定的消 息头。
1. 所以,如果要把 SESSION BEAN 发布为 WEB 服务,必须在 JDK 1.5 下做,安装 MyEclipse 的话, JDK 会自动被升级到 1.6, 所以要先改回来。
Project->properties->java build path, 选中 JDK EDIT
 
Alternate JRE 中选 JDK 1.5 ,注意,如果是自己的 JDK ,版本一定不能高过 BEA 带的 JDK
 
2. 添加注释
@JndiName (remote= "han.interfaces.HelloWorldBean" )
@WebService (name= "HelloWorldPortType" ,              // 端口名
        serviceName= "HelloWorldService" ,              // 服务名
        targetNamespace= "http://ejb.han" )            //WebService 注释是必须有的,下面的 WLHttpTransport SOAPBinding 是推荐的
@WLHttpTransport (contextPath= "Hello_EJB" ,        
             serviceUri= "HelloWorldService" ,          //http://localhost:7001/contextPath/services/serviceUri?wsdl
             portName= "HelloWorldPort" )
@SOAPBinding (style= SOAPBinding .Style. DOCUMENT ,   // 好象都这么写
    use= SOAPBinding .Use. LITERAL ,
    parameterStyle= SOAPBinding .ParameterStyle. WRAPPED )
添加业务逻辑注释
    @RemoteMethod ()
    @WebMethod ()
导入相关包
import weblogic.jws.WLHttpTransport;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import weblogic.ejbgen.JndiName;
import weblogic.ejbgen.RemoteMethod;
import weblogic.jws.WLHttpTransport;
 
3. 创建 build.xml 文件(注释是后写的,所以不符合规范)
<project name= "webservice-HelloWorld" default= "all" >             // 工程(默认的任务是 all
    <property name= "example-output" value= "output" />          
    <property name= "ear-dir" value= "${example-output}" />
    <property name= "src-dir" value= "./src" />
    <taskdef name= "jwsc" classname= "weblogic.wsee.tools.anttasks.JwscTask" /> //Ant 任务
    <target name= "all" depends= "clean,build-service" />             //target:all
    <target name= "clean" >                                                          //target:clean
       <delete dir= "${example-output}" />                                     // 文件目录
    </target>
    <target name= "build-service" depends= "                              //target:build-service
       <jwsc srcdir= "${src-dir}" destdir= "${ear-dir}" >                     // 源目录和目标目录
           <module>                                                                      // 添加 jws 文件(可以多个)
              <jws file= "han/ejb/HelloWorldBean.java" />                /jws 文件(包含注释的文件
           </module>
       </jwsc>
     </target>
</project>
 
4.Run Ant
 
会发现生成了两个文件(application.xml和weblogic-application.xml)和一个JAR(jws.jar)包
 
5. 部署
启动服务器,进入console,手动部署
 
处于ACTIVE状态
然后写个客户断就可以测试了

你可能感兴趣的:(webservice,职场,休闲)