ServiceMix入门(4):在servicemix中部署自己的第一个webservice接口

首先我们先开发一个简单的webservice接口,可以参见  开发并发布一个简单的webservice接口 ,本文也是在 开发并发布一个简单的webservice接口 的基础上,接着编写的。

  1. pom文件依赖如下,servicemix中支持部署的是bundle的jar,故需要加入bunndle的打包插件依赖
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.demo</groupId>
      <artifactId>ws-demo</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>bundle</packaging>
      
      <properties>
    		<java-version>1.7</java-version>
    		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    		<camel-version>2.16.1</camel-version>
    		<cxf-version>3.1.4</cxf-version>
    	</properties>
    
    	<dependencies>
    		<dependency>
    			<groupId>org.apache.camel</groupId>
    			<artifactId>camel-core</artifactId>
    			<version>${camel-version}</version>
    		</dependency>
    		<dependency>
    			<groupId>org.apache.camel</groupId>
    			<artifactId>camel-cxf</artifactId>
    			<version>${camel-version}</version>
    		</dependency>
    		<dependency>
    			<groupId>org.apache.camel</groupId>
    			<artifactId>camel-http</artifactId>
    			<version>${camel-version}</version>
    		</dependency>
    
    		<dependency>
    			<groupId>org.apache.cxf</groupId>
    			<artifactId>cxf-core</artifactId>
    			<version>${cxf-version}</version>
    		</dependency>
    
    		<!-- scope设置为provided,是因为只有在本地测试发布的时候用的到,部署到servicemix中不需要这两个jar -->
    		<dependency>
    			<groupId>org.apache.camel</groupId>
    			<artifactId>camel-jetty</artifactId>
    			<version>${camel-version}</version>
    			<scope>provided</scope>
    		</dependency>
    		<dependency>
    			<groupId>org.apache.cxf</groupId>
    			<artifactId>cxf-rt-transports-http-jetty</artifactId>
    			<version>${cxf-version}</version>
    			<scope>provided</scope>
    		</dependency>
    
    	</dependencies>
    
    	<build>
    		<finalName>ws-demo</finalName>
    		<plugins>
    			<plugin>
    				<groupId>org.apache.maven.plugins</groupId>
    				<artifactId>maven-compiler-plugin</artifactId>
    				<version>3.5.1</version>
    				<configuration>
    					<source>${java-version}</source>
    					<target>${java-version}</target>
    				</configuration>
    			</plugin>
    
    			<plugin>
    				<groupId>org.apache.felix</groupId>
    				<artifactId>maven-bundle-plugin</artifactId>
    				<version>3.5.0</version>
    				<extensions>true</extensions>
    				<configuration>
    					<source>${java-version}</source>
    					<target>${java-version}</target>
    					<encoding>${project.build.sourceEncoding}</encoding>
    					<instructions>
    						<Import-Package>*</Import-Package>
    					</instructions>
    				</configuration>
    			</plugin>
    		</plugins>
    	</build>
    </project>

     

  2. 新增process
    /**
     * 
     * 文件名:CustomerProcess.java
     */
    package com.demo.service.process;
    
    import java.lang.reflect.Method;
    
    import org.apache.camel.Exchange;
    import org.apache.camel.Processor;
    import org.apache.camel.component.cxf.common.message.CxfConstants;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    /**
     * 自定义处理类
     * @author Monk
     * @version V1.0
     * @date 2019年6月28日 上午11:13:31
     */
    public class CustomerProcess implements Processor{
        
        private static Logger logger = LoggerFactory.getLogger(CustomerProcess.class);
        
        private Class<?> beanClass;
        private Object instance;
    
        public CustomerProcess(Object object) {
            beanClass = object.getClass();
            instance = object;
        }
    
        @Override
        public void process(Exchange exchange) throws Exception {
            String inputMessage = exchange.getIn().getBody(String.class);
            logger.info("inputMessage : " + inputMessage);
    
            String operationName = exchange.getIn().getHeader(CxfConstants.OPERATION_NAME, String.class);
            Method method = findMethod(operationName, exchange.getIn().getBody(Object[].class));
            Object response = method.invoke(instance, exchange.getIn().getBody(Object[].class));
    
            exchange.getOut().setBody(response);
            
        }
        
        /**
         * 通过JAVA反射获取对应执行的方法
         * @param operationName  操作名称也及方法名
         * @param parameters 参数
         * @return 方法
         * @throws SecurityException
         * @throws NoSuchMethodException
         * @author Monk
         * @date 2019年6月29日 上午12:05:32
         */
        private Method findMethod(String operationName, Object[] parameters) throws SecurityException, NoSuchMethodException {
            return beanClass.getMethod(operationName, getParameterTypes(parameters));
        }
    
        /**
         * 获取参数类型,提供给findMethod()方法使用,根据方法名和参数类型获取对应的方法
         * @param parameters 参数
         * @return
         * @author Monk
         * @date 2019年6月29日 上午12:06:19
         */
        private Class<?>[] getParameterTypes(Object[] parameters) {
            if (parameters == null) {
                return new Class[0];
            }
            Class<?>[] answer = new Class[parameters.length];
            int i = 0;
            for (Object object : parameters) {
                answer[i] = object.getClass();
                i++;
            }
            return answer;
        }
    }
    

     

  3. 新增路由
    /**
     * 
     * 文件名:CustomerRoute.java
     */
    package com.demo.service.route;
    
    import org.apache.camel.builder.RouteBuilder;
    
    import com.demo.service.impl.CalculatorServiceImpl;
    import com.demo.service.process.CustomerProcess;
    
    /**
     * 自定义路由类
     * @author Monk
     * @version V1.0
     * @date 2019年6月28日 上午11:09:48
     */
    public class CustomerRoute extends RouteBuilder{
    
        @Override
        public void configure() throws Exception {
           from("cxf://http://localhost:9082/CalculatorService?serviceClass=com.demo.service.CalculatorService").process(new CustomerProcess(new CalculatorServiceImpl()));
        }
    
    }
    

     

  4. 配置camel-spring.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"
           xsi:schemaLocation=
                   "http://www.springframework.org/schema/beans
                   http://www.springframework.org/schema/beans/spring-beans.xsd
                   http://camel.apache.org/schema/spring
                   http://camel.apache.org/schema/spring/camel-spring.xsd">
                   
         <bean id = "calculatorServiceImpl" class="com.demo.service.impl.CalculatorServiceImpl"></bean>
    
        <bean id="csutomerProcess" class="com.demo.service.process.CustomerProcess">
        	<constructor-arg name="object" ref="calculatorServiceImpl">
        	</constructor-arg>
        </bean>
    
        <camelContext id="testCamelContext" xmlns="http://camel.apache.org/schema/spring">
            <route>
                <from uri="cxf://http://localhost:9082/CalculatorService?serviceClass=com.demo.service.CalculatorService"/>
                <process ref="csutomerProcess"/>
            </route>
        </camelContext>
    
    </beans>
    

     

  5. 在cmd窗口执行mvn打包命令  mvn clean package,将project_home/target目录下的ws-demo.jar复制到servicemix_home/deploy中完成部署
  6. 访问地址  http://localhost:9082/CalculatorService?wsdl  进行测试。
  7. 附件中有代码压缩包,仅供参考

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值