在这里我所实现的一个功能是点击一个按钮需要将PCS工程的数据传递到PIS然后交由其对数据进行更新操作。
1、PIS上的相关配置与文件 (服务器端)
web.xml 里添加下述相关内容
<!--与PCS工程交互的配置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:/META-INF/**/spring-*.xml,classpath*:/META-INF/**/remote-servlet.xml</param-value> </context-param> <!-- spring 加载监视器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <listener> <listener-class>com.yihaodian.pis.webapp.listener.AppRequestListener</listener-class> </listener> <servlet> <servlet-name>remote</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>remote</servlet-name> <url-pattern>/remoting/*</url-pattern> </servlet-mapping> <!-- 另一个工程的配置--> <servlet> <description>HTTP Exporters</description> <servlet-name>services</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>services</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping>
remote-servlet.xml 的内容 将PIS的BEAN暴露给PCS然PCS对其进行调用
<?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-2.5.xsd"> <bean id="rmiService" class="com.yihaodian.pis.service.testremoteinvoke.IRemoteServiceImpl"></bean> <bean class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter" name="/remoteService"> <property name="service" ref="rmiService" /> <property name="serviceInterface" value="com.yihaodian.pis.service.testremoteinvoke.IRemoteService"/> </bean> </beans>
接口以及其实现类(有PCS对其实现类的方法进行调用)
实现类IRemoteServiceImpl。java:
package com.yihaodian.pis.service.testremoteinvoke; import java.util.Date; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.apache.commons.lang.StringUtils; import org.apache.log4j.Logger; import org.apache.struts2.ServletActionContext; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.newheight.service.webservice.dto.BackOperator; import com.yihaodian.pis.dto.ProductHistoryDTO; import com.yihaodian.pis.dto.TaskDto; import com.yihaodian.pis.service.TaskService; public class IRemoteServiceImpl implements IRemoteService { private static final Logger logger = Logger .getLogger(IRemoteServiceImpl.class); public String getString(String msg) { String str = "正在请求调用。。。远程服务调用成功" + msg; return str; } public String getProduct(String siteid, String productCode) { ApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "classpath*:/META-INF/**/spring-*.xml" }); TaskService taskSvc = (TaskService) context.getBean("taskSvc"); //当数据很多时就会出现数组指数出界 String[] site = siteid.split(","); String[] productCodes = productCode.split(","); int len = site.length; Integer[] taskIds = new Integer[len]; for (int i = 0; i < len; i++) { TaskDto taskDto = new TaskDto(); String codes = productCodes[i]; if (null != codes && !"".equals(codes)) { codes = StringUtils.replace(codes, "\r\n", System.getProperty("line.separator")); taskDto.setProductCodes(codes); } taskDto.setRunStatus(TaskDto.RunStatus.BEGIN.ordinal()); taskDto.setProcessStatus(TaskDto.ProcessStatus.WAIT.ordinal()); // 获取已登录用户的信息 // Long userId = ((BackOperator) ServletActionContext.getRequest() // .getSession().getAttribute("admin")).getId(); Long userId = (long) 6660; taskDto.setUserId(userId); taskDto.setStartTime(new Date()); taskDto.setName("PCS比价运行第" + (i + 1) + "任务"); taskDto.setDescription("PCS抓价任务"); taskDto.setType(2); taskDto.setSiteId(Integer.parseInt(site[i])); taskDto.setTaskId(taskSvc.addTask(taskDto)); taskIds[i] = taskDto.getTaskId(); try { taskSvc.batchFetchProductPrice(taskDto.getTaskId()); } catch (Exception e) { logger.error("批量抓价出错:", e); } } // 在此处检查任务的完成情况。 int allTime =0; int doneTask = 0; for (int i = 0; i < len; i++) { boolean index = true; while (index) { TaskDto task = taskSvc.getTaskById(taskIds[i]); if (task.getRunStatus() == 4) { logger.info("任务" + taskIds[i] + "已经完成!"); taskSvc.persistPriceJob(taskIds[i]); doneTask++; index = false; } else { if(allTime<60){//超时 allTime =allTime + 5; sleep(10000);//睡眠一段时间后继续检查看是否完成任务 }else break; } } } if(doneTask<len) return "未能正常完成更新,可能有以下几个原因,1、当前任务太多,出现任务超时,2、PIS爬虫不够,3、系统异常"; else return "恭喜,圆满完成任务!"; } public void sleep(long ms) { try { Thread.sleep(ms); } catch (InterruptedException ex) { ex.printStackTrace(System.out); } } public static Logger getLogger() { return logger; } }
2、PCS工程的相关文件与配置
接口类IRemoteService。java
package com.yihaodian.pis.service.testremoteinvoke; import java.util.List; import com.yihaodian.pcs.dto.ProductHistoryDTO; public interface IRemoteService { public String getString(String msg); public String getProduct(String siteid,String productCode); }
在SPING里新建一个用来调用方法的BEAN
<!-- 向旗添加PCS与PIS数据交互 --> <bean id="remoteService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean"> <property name="serviceUrl" value="http://127.0.0.1:8080/pis1.2/remoting/remoteService"/> <property name="serviceInterface" value="com.yihaodian.pis.service.testremoteinvoke.IRemoteService"/> </bean>
以上VALUE属性与PIS的IP和WEB.XML里的配置有关,另外如果一台电脑无法运行两个工程,可以更改服务器的端口号。
在这里我将PCS的端口号改为8880,直接能够在TOMCAT上运行。
然后再ACTION得到上述的BEAN对象对接口映射到PIS的方法进行调用
ApplicationContext beanAccontext = WebApplicationContextUtils .getRequiredWebApplicationContext(ServletActionContext .getServletContext()); IRemoteService reService = (IRemoteService) beanAccontext.getBean("remoteService"); //以下为返回的结果。 String mString= ""; try { mString= reService.getProduct(siteAll,proCodeAll); } catch (Exception e) { e.printStackTrace(); } logger.info(mString);
不懂的联系QQ526151410