配置如下:
①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();
Spring Http Invoker,布布扣,bubuko.com
时间: 2024-10-05 04:26:01