微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

毕业一年总结专题6--Tuscany1.6发布WebService和hessian


关于框架的文章这是最后一篇了,这是公司目前发布服务的方式,我把它抽出来。发布的配置不复杂。


要点一:Tuscnay1.6将所发布的服务绑定为hessian时,如果所用的hessian版本3.1.3,会报:Exception in thread "main" com.caucho.hessian.client.HessianRuntimeException: com.caucho.hessian.io.HessianProtocolException: 500,用的是4.0.7就可以

要点二:服务器是jboss eap 4.3

要点三:  加入j2ee5的几个lib包,貌似有javaee.jar就OK

要点四:使用整合spring对webservice调用时,会报“Caused by: javax.xml.ws.WebServiceException: Unable to create JAXBContext”,原因是传输的类型太复杂了(Map算复杂对象么),如果用简单的string方式,证明是成功的,这点hessian做的比较好

以下为Spring调用WebService服务的配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-2.5.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
	http://www.springframework.org/schema/tx
	http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    
	<bean id="myServiceCtrl" class="org.springframework.remoting.jaxws.JaxWsPortProxyfactorybean">
		<property name="serviceInterface" value="com.shine.soa.MyServiceCtrl"/>
		<property name="wsdlDocumentUrl" value="http://10.168.2.56:8080/FrameWrok/ws/FrameWrok_MyServiceCtrl?wsdl"/>
	    <property name="namespaceUri" value="http://soa.shine.com/"/>
		<property name="serviceName" value="MyServiceCtrlService"/>
		<property name="portName" value="MyServiceCtrlPort"/>
	</bean>

	
</beans>

这一篇代码我会黏贴得详细点,原来CSDN不允许发附件啊,还以为我等级低呢


第一部分是代码:位置为src/main/java

package com.shine.soa;

/**
*  POJO
*/
 public class Message implements java.io.Serializable{
    
    
     
    /**
     * 
     */
    private static final long serialVersionUID = 1735228251595190659L;
    private String name;
    private Integer age;
    private Boolean married;
    /**
     * @return the name
     */
    public String getName() {
        return name;
    }   
    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }
    /**
     * @return the age
     */
    public Integer getAge() {
        return age;
    }
    /**
     * @param age the age to set
     */
    public void setAge(Integer age) {
        this.age = age;
    }
    /**
     * @return the married
     */
    public Boolean getMarried() {
        return married;
    }
    /**
     * @param married the married to set
     */
    public void setMarried(Boolean married) {
        this.married = married;
    }
    
    

}


接口:

package com.shine.soa;

import java.util.Map;

import javax.jws.WebService;

//注解是必须的哦
@WebService
public interface MyServiceCtrl {
    //heassian完全支持
    Message getMessage(String name,Integer age,Boolean married);
    //heassian完全支持
    Map<String,Object> getMap();
    //简单的数据类型,才可以用spring支持的方式调用webservice
    String getString(String name);
}

实现类:

/**
 * 
 */
package com.shine.soa;

import java.util.HashMap;
import java.util.Map;


/**
 * 
 *
 * <pre>
 * 修改日期		修改修改原因
 * 2013-2-8	zgy	新建
 * </pre>
 */
public class MyServiceCtrlImpl implements MyServiceCtrl {

    /* (non-Javadoc)
     * @see com.shine.soa.MyServiceCtrl#getString(java.lang.String)
     */
    @Override
    public String getString(String name) {
        return "Hello : " +name;
    }

    /* (non-Javadoc)
     * @see com.shine.soa.MyServiceCtrl#getMessage(java.lang.String,java.lang.Integer,java.lang.Boolean)
     */
    @Override
    public Message getMessage(String name,Boolean married) {
        Message message = new Message();
        message.setName("name:"+name);
        message.setAge(age*2);
        message.setMarried(married);
        return message;
    }

    /* (non-Javadoc)
     * @see com.shine.soa.MyServiceCtrl#getMap()
     */
    @Override
    public Map<String,Object> getMap() {
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("1",1);
        map.put("ZGY","LHM");
        Message message = new Message();
        message.setAge(1);
        message.setMarried(true);
        message.setName("LDH");
        map.put("message",message);
        return map;
    }
    
    

}

自定义一个监听类:

package com.shine.soa;

import javax.servlet.ServletContextEvent;
import javax.servlet.servletcontextlistener;

import org.apache.tuscany.sca.host.embedded.SCADomain;
import org.apache.tuscany.sca.host.webapp.WebAppServletHost;

/**
 * 
 * 类说明:用于初始化SCADomain对象的Servlet上下文监听器,若需要使用SCA服务,则需要在指定WEB中配置此SCA上下文.
 * 
 * <pre>

 * </pre>
 */
public class ScaWebContextinitListener implements servletcontextlistener {


    /**
     * 
     * 构造器
     * 
     * <pre>
     * 修改日期      修改修改原因

     * </pre>
     */
    public ScaWebContextinitListener() {
    }

    /**
     * 
     * 上下文初始化,获取上下文中的ScaDomain
     * 
     */
    public void contextinitialized(ServletContextEvent event) {
        SCADomain scaDomain = (SCADomain) event.getServletContext()
                .getAttribute(WebAppServletHost.SCA_DOMAIN_ATTRIBUTE);
        if (scaDomain == null) {
            System.err.print("ServletContext上下文未找到SCADomain!");
        } else {
            System.err.print("成功加载SCADomain对象.");
        }
    }

    /**
     * contextDestroyed.
     * 
     * @see servletcontextlistener#contextDestroyed(ServletContextEvent)
     */
    public void contextDestroyed(ServletContextEvent arg0) {

    }
}

下面是位于src/main/resources下的三个配置文件,非常关键

一个配置文件是applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-2.5.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
	http://www.springframework.org/schema/tx
	http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    
    <!--这里可以加入自己的特殊处理-->
	
	
</beans>

第二个文件是SCA服务定义文件:spring-sca.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:sca="http://www.springframework.org/schema/sca" xsi:schemaLocation="http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans-2.0.xsd     http://www.springframework.org/schema/sca     http://www.osoa.org/xmlns/sca/1.0/spring-sca.xsd">
  <import resource="applicationContext.xml" />
  <bean id="myServiceCtrlImpl" class="com.shine.soa.MyServiceCtrlImpl" />
  <sca:service name="MyServiceCtrl" type="com.shine.soa.MyServiceCtrl" target="myServiceCtrlImpl" />
</beans>


第三个文件是SCA服务的发布文件:scaservices.composite

<?xml version="1.0" encoding="UTF-8"?>
<composite xmlns="http://www.osoa.org/xmlns/sca/1.0" xmlns:t="http://tuscany.apache.org/xmlns/sca/1.0" xmlns:rmi="http://tuscany.apache.org/xmlns/binding/rmi/1.0-SNAPSHOT" xmlns:e="http://hessian" xmlns:s="http://app.shine.com/FrameWrok/services" targetNamespace="http://app.shine.com/FrameWrok/services" name="FrameWrok">
  <component name="SpringServices">
    <implementation.spring location="WEB-INF/classes/spring-sca.xml" />
    <!-- services here -->
    <service name="MyServiceCtrl">
      <binding.ws uri="/ws/FrameWrok_MyServiceCtrl" />
      <e:binding.hessian uri="/hessian/FrameWrok_MyServiceCtrl" />
    </service>
  </component>
</composite>


下面的这个配置文件位于src/main/webapp/meta-inf/sca-contribution.xml,文件中指定某个构件可以部署

<?xml version="1.0" encoding="UTF-8" ?>
<contribution xmlns="http://www.osoa.org/xmlns/sca/1.0" 
    xmlns:sjs="http://app.shine.com/FrameWrok/services">
   <deployable composite="sjs:FrameWrok"/>
 </contribution>

到最后是web.xml,在这里可以看到tuscany的影子

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	
	<!-- Tuscany 配置 -->
    <listener>
        <listener-class>org.apache.tuscany.sca.host.webapp.TuscanyContextListener</listener-class>
    </listener>

    <filter>
        <filter-name>tuscany</filter-name>
        <filter-class>org.apache.tuscany.sca.host.webapp.TuscanyServletFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>tuscany</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

	
	<!-- 自定义SCA服务加载监听 -->
	<listener>
		<listener-class>com.shine.soa.ScaWebContextinitListener</listener-class>
	</listener>

	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
	


</web-app>

大家别嫌弃哈,还有最后将整个环境需要用到的jar包列出来,Maven要用到私服服务器,应用很难跑起来的。jar包分两种,一种是编译时候用到的放在build路径,另外一种是启动应用用到的,放在服务器的lib或者应用的WEB-INF/lib。jar包其实就是环境,能跑起来就不必细究,跑不起来那就要看看是否jar包冲突啊,版本啊,缺包啊,等等。

这是build用到的jar包:

这是WEB-INF用到的jar包,此外还需用到jboss下的公共lib包:


服务启动了,总得让我用才证明成功了吧,其实可以先看看wsdl,:http://127.0.0.1:8080/FrameWrok/ws/FrameWrok_MyServiceCtrl?wsdl

        //使用代码访问hessian
        HessianProxyFactory hpf = new HessianProxyFactory();
        MyServiceCtrl szctrl = (MyServiceCtrl)hpf.create("http://localhost:8080/FrameWrok/hessian/FrameWrok_MyServiceCtrl");
        Message message = szctrl.getMessage("FUCK",1,false);
        System.out.println(message.getName()+message.getAge()+message.getMarried());
        Map<String,Object> map = szctrl.getMap();
        System.out.println(map.get("1"));
        System.out.println(map.get("ZGY"));
        Message ldh = (Message)map.get("message");
        System.out.println(ldh.getName()+ldh.getAge()+ldh.getMarried());
概念在下一篇,大家肯定都看烦了

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐