java系统之间的调用
来源:互联网 发布:java标识符的命名测试 编辑:程序博客网 时间:2024/05/24 15:36
不同的系统之间需要交互,需要数据的共享,总结一下主要的几种方法:
1、HttpURLConnection建立连接
public static String openUrlReturnMoreMessage(String strUrl, String charSet) throws NetConnectionException{String result = null;BufferedReader br = null;try{URL webUrl = new URL(strUrl);HttpURLConnection httpConn = (HttpURLConnection) webUrl.openConnection();httpConn.setConnectTimeout(30000);httpConn.setReadTimeout(30000);if (httpConn.getResponseCode() != HttpURLConnection.HTTP_OK){log.warn(strUrl + "|ResponseCode=" + httpConn.getResponseCode());throw new NetConnectionException("Connect to url[" + strUrl + "] error, response code is "+ httpConn.getResponseCode());}StringBuffer sb = new StringBuffer();String tempStr = "";br = new BufferedReader(new InputStreamReader(httpConn.getInputStream(), charSet));while ((tempStr = br.readLine()) != null){sb.append(tempStr + "\n");}return sb.toString();}catch (Exception e){//获取?前的url部分,来区分不同的应用int endIndex = strUrl.indexOf("?");String errorUrl = strUrl;if(endIndex != -1){errorUrl = strUrl.substring(0,endIndex);}LogService.fatal("connect error[" + errorUrl + "]",e,"HttpUtil:" + errorUrl);throw new NetConnectionException("Connect to url[" + strUrl + "] error");}finally{if (br != null){ try{br.close();}catch (IOException e1){LoggerUtil.error(e1, HttpUtil.class.getName(), "openUrl(String strUrl)");}}}}
2、Runtime.getRuntime().exec(参数)
通过执行脚本,结果参数意义如下:
"1", "刷新成功"
"-1", "刷新前台时,前台系统异常"
"-2", "刷新失败,前台数据库异常"
"-3", "刷新失败,类型参数为空"
"-4", "刷新失败,无效类型"
"-5", "刷新失败,无效Ip"
Process pro = Runtime.getRuntime().exec("sh /home/shell/my_mem.sh 10.120.30.35:6801 VersionConstant");//执行脚本,两个参数,第一个:10.120.30.35:6801//第二个参数:VersionConstantBufferReader br=new BufferedReader(newInputStreamReader(pro.getInputStream())); StringBuffer sb = new StringBuffer();while ((line = br.readLine()) != null){sb.append(line);}String message = sb.toString();//message为1、-1、-2、-3、-4、-5中的某一个参数,表示不同的含义脚本中调用接口
#!/bin/shSHELL_PATH=/home/shellShell="/usr/bin/wget -O $SHELL_PATH/my_flush.log --no-check-certificate --timeout=10 -t1 http://$1/servlet/refreshConstant?type=$2"#exec shell$Shell#get resultcat $SHELL_PATH/epay_flush.log然后在目标机器上的项目中配置/servlet/refreshConstant的servlet,在对应的servlet中接受参数type,并进行处理;
3、webservice
可以采用cxf和spring结合的例子。
首先配置服务器端,经历了一下五个步骤:
(1)发布一个接口IHelloWorld.java:
package cn.com.service; import javax.jws.WebParam; import javax.jws.WebService; @WebService public interface IHelloWorld { //@WebParam(name="arg0")可有可无,为了增强可读性 public String sayHello(@WebParam(name="arg0")String text); }(2)增加接口的实现类HelloWorldImpl:
package cn.com.service; import javax.jws.WebService; @WebService(endpointInterface="cn.com.service.IHelloWorld") public class HelloWorldImpl implements IHelloWorld { public String sayHello(String text) { return "Hello" + text ; } }(3)现在可以进行spring配置了,在eclipse的src文件夹下新建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:p="http://www.springframework.org/schema/p" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:cxf="http://cxf.apache.org/core" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <import resource="classpath:META-INF/cxf/cxf.xml" /> <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /> <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> <bean id="hello" class="cn.com.service.HelloWorldImpl"/> <jaxws:endpoint id="helloWorld" implementor="#hello" address="/<span style="color:#FF6666;">HelloWorld</span>" /> </beans>注意:加命名空间;cxf.xml,cxf-extension-soap.xml,cxf-servlet.xml三个文件都在cxf.jar中把它们拷贝到META-INF/目录下;
(5)配置web.xml
<?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"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>CXFServlet</servlet-name> <display-name>CXF Servlet</display-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>接着配置客户端:
(1)新建客户程序test
package pro.webws.client; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-client.xml"); IHelloWorld client = (IHelloWorld) ctx.getBean("<span style="color:#FF6666;">client</span>"); String result = client.sayHello("你好!"); System.out.println(result); } } (2)配置客户端的spring文件了,新建spring-client.xml文件<pre name="code" class="html"> <?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:p="http://www.springframework.org/schema/p" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:cxf="http://cxf.apache.org/core" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd"> <bean id="<span style="color:#FF0000;">client</span>" class="pro.webws.client.IHelloWorld" factory-bean="clientFactory" factory-method="create" /> <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean"> <property name="serviceClass" value="pro.webws.client.IHelloWorld" /> <property name="address" value="http://localhost:8080/webws/<span style="color:#FF0000;">HelloWorld</span>" /> </bean> </beans> 至此完成
0 0
- java系统之间的调用
- 信号和系统调用之间的关系
- 系统调用、API之间的关系
- Java的系统调用
- java和cmd之间的调用
- Java 类之间方法调用的总结
- Java Applet之间的互相调用
- [Java]包与包之间的调用
- 系统调用和c库之间的关系
- 系统调用和c库之间的关系
- 系统调用、API之间的关系(图)
- 系统调用和c库之间的关系
- 系统调用和c库之间的关系
- 系统调用和c库之间的关系
- 系统调用和C库之间的关系?
- 系统调用和c库之间的关系
- 应用程序和VFS之间的接口是系统调用
- 系统调用和c库之间的关系
- Linux系统信息查看命令
- unity 编译安卓出错
- activity主窗口与软键盘的交互模式,可以用来避免输入法面板遮挡问题,Android1.5后的一个新特性。
- Linux 磁盘分区中的UUID
- 简单文件操作命令
- java系统之间的调用
- 分页 很实用
- 图片的一些效果处理方法
- 冒泡排序和选择法排序
- 个人对码表的理解(ASCII,GBK,Unicode,UTF-8等)。
- 位置与地图(三)给地图添加覆盖层
- kkk
- 流媒体服务器
- Oracle数据库技术