您的位置:首页 > 编程语言 > Java开发

java系统之间的调用

2014-06-30 17:31 113 查看
不同的系统之间需要交互,需要数据的共享,总结一下主要的几种方法:

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
//第二个参数:VersionConstant
BufferReader 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/sh
SHELL_PATH=/home/shell
Shell="/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 result
cat $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>
至此完成



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: