Spring Http Invoker

简介:

配置例如以下:

①web.xml配置

    <servlet>  
  
        <servlet-name>remote</servlet-name>  
  
       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  
        <!-- 配置该Servlet随应用启动时候启动 -->  
  
        <load-on-startup>1</load-on-startup>  
  
    </servlet>  
  
	<!-- 配置DispatcherServlet映射的url -->  
 
    <servlet-mapping>  
  
       <servlet-name>remote</servlet-name>  
  
       <url-pattern>/remoting/*</url-pattern>  
  
    </servlet-mapping>  

②client.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans default-lazy-init="true">
<bean id="sync.BackupSearchService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
		<property name="serviceUrl" value="http://${juyuan.webservice.address}:${juyuan.webservice.port}/${juyuan.webservice.application}/remoting/backupSearchService"/>
		<property name="serviceInterface" value="com.juyuan.service.IBackupSearchService"/>
</bean>
 

</beans>


③remote.xml

<?xml version="1.0" encoding="UTF-8"?

> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter" name="/backupSearchService"> <property name="service" ref="BackupSearchService"/> <property name="serviceInterface" value="com.juyuan.service.IBackupSearchService"/> </bean> <bean class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter" name="/backupStrategyService"> <property name="service" ref="BackupStrategyService"/> <property name="serviceInterface" value="com.juyuan.service.IBackupStrategyService"/> </bean> </beans>



④SyncService

package com.juyuan.service.impl;

import java.lang.reflect.Method;
import java.util.Date;
import com.juyuan.model.Alarms;
import com.juyuan.model.AlarmsKeys;
import com.juyuan.model.Logs;
import com.juyuan.model.LogsKeys;
import com.juyuan.service.IAlarmsService;
import com.juyuan.service.ILogsService;
import com.juyuan.support.AppCommon;
import com.juyuan.support.AppContext;

/**
 * add by LB 已屏蔽.若要开启同步请去掉execute里凝视的代码.
 * @author LB
 *
 */
public class SyncService implements Runnable{
	
	
	private Method method = null;
	private Object serviceObj = null;
	private Object[] paramArray = null;
	/**
	 * 同步数据通用类
	 * @param serviceObj	服务对象.
	 * @param methodName	待调用的服务方法.
	 * @param paramTypes	參数的类类型数组.
	 * @param paramArray	參数数组.(必须可以序列化)
	 */
	public SyncService(Object serviceObj,String methodName,Class<Object>[] paramTypes,Object[] paramArray){
		try {
			this.serviceObj = serviceObj;
			this.paramArray = paramArray;
			method = serviceObj.getClass().getMethod(methodName, paramTypes );
		}  catch (Exception e) {
			
		}
		
	}
	
	public void run() {
		try {
			method.invoke(serviceObj,paramArray);
		} catch (Exception e) {
			e.printStackTrace();
			
		}
	}
	/**
	 * 运行同步
	 */
	public void execute(){
		
		new Thread(this).start();
	}
}


⑤调用方式:

new SyncService(this.getSyncConditionService(),"saveSearchCondition",new Class[]{DefaultSearchCondition.class},new Object[]{entity}).execute();








本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5227023.html,如需转载请自行联系原作者


相关文章
|
7月前
|
Web App开发 监控 Java
|
7月前
|
Java Spring
用spring发送http请求
用spring发送http请求
|
7月前
spring3 springfox报错Type javax.servlet.http.HttpServletRequest not present
spring3 springfox报错Type javax.servlet.http.HttpServletRequest not present
754 0
|
2月前
|
负载均衡 Java 开发者
Spring Cloud 远程调用:为何选择 HTTP 而非 RPC?
【10月更文挑战第1天】在微服务架构中,远程服务调用是一个核心环节。面对HTTP和RPC(Remote Procedure Call,远程过程调用)这两种通信协议,Spring Cloud 选择了HTTP作为其主要通信手段。本文将深入探讨Spring Cloud选择HTTP而非RPC的原因,以及这一选择在实际工作中的优势。
94 0
|
5月前
|
Java Spring
spring restTemplate 进行http请求的工具类封装
spring restTemplate 进行http请求的工具类封装
224 3
|
5月前
|
Java Spring
spring cloud gateway在使用 zookeeper 注册中心时,配置https 进行服务转发
spring cloud gateway在使用 zookeeper 注册中心时,配置https 进行服务转发
115 3
|
6月前
|
Java API Spring
Spring Boot中使用Feign进行HTTP请求
Spring Boot中使用Feign进行HTTP请求
|
6月前
|
JSON 前端开发 Java
记录一次让我吐血的spring3 MVC HTTP406 Json转换错误
记录一次让我吐血的spring3 MVC HTTP406 Json转换错误
33 0
|
7月前
|
JSON Java Apache
Spring Cloud Feign 使用Apache的HTTP Client替换Feign原生httpclient
Spring Cloud Feign 使用Apache的HTTP Client替换Feign原生httpclient
399 0