最近做一个项目,用spring的httpinvoker远程调用方式,配置成功了。。一切没有问题。为了更加完美,考虑到假如服务提供方的服务出现问题,链接不上,那么客户端连接就一直会等待,这样对客户来说,是很不好的方式,,,10几秒没有反映。。。最后还会报cannot connect to 某某 ip 某某服务。。
不知道httpinvoker对服务的超时问题可以在什么地方配置不?
为了大家了解我配置的httpinvoker。把相关的代码贴出来。。。
首先是服务器端:
web.xml
[code=Java]
<servlet>
<servlet-name>remote</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
[/code]
remote-servlet.xml
[code=Java]
</bean>
<bean name="/requestXmlData" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
<property name="service">
<ref bean="xml"/>
</property>
<property name="serviceInterface">
<value>com.eway.wap.remote.businessinterface.RequestParser</value>
</property>
</bean>
[/code]
spring的配置文件中
[code=Java]
<!--httpinvoker服务器端配置-->
<bean id="xml" class="com.eway.wap.remote.businessinterface.RequestParserImpl"/>
[/code]
客户端:
[code=Java]
<!--httpinvoker调用远程服务-->
<bean id="httpInvokerRequestParser"
class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
<property name="serviceUrl" value="eway"/>
<property name="httpInvokerRequestExecutor" ref="httpInvokerRequestExecutor"/>
<property name="serviceInterface" value="com.eway.wap.remote.businessinterface.RequestParser"/>
</bean>
<bean id="httpInvokerRequestExecutor" class="com.eway.wap.remote.invoke.httpinvoker.EwayHttpInvokerRequestExecutor"/>
[/code]
------------------------------
附录:
使用到的java接口和类如下:
com.eway.wap.remote.businessinterface.RequestParser
[code=Java]
package com.eway.wap.remote.businessinterface;
import com.eway.wap.exception.sub.NotGetRemoteDataHttpInvokerException;
/**
* Created by IntelliJ IDEA.
* User: yuchen
* Date: 2008-9-24
* Time: 10:17:17
* To change this template use File | Settings | File Templates.
*/
public interface RequestParser {
/**
*
* @param _request
* @return
* @throws NotGetRemoteDataHttpInvokerException
*/
public String parse(String _request) ;
}
[/code]
[code=Java]
package com.eway.wap.remote.businessinterface;
import com.eway.wap.exception.sub.NotGetRemoteDataHttpInvokerException;
import com.eway.wap.remote.axis.AxisJavaCallDotNet;
/**
* Created by IntelliJ IDEA.
* User: yuche
* Date: 2008-9-24
* Time: 10:40:19
* To change this template use File | Settings | File Templates.
*/
public class RequestParserImpl implements RequestParser {
public String parse(String requestXml) {
System.out.println("***************************************requestXml:"+requestXml);
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<response>\n" +
"\t<sysid>0001</sysid>\n" +
"\t<sysname></sysname>\n" +
"\t<user>123123</user>\n" +
"\t<type>wapdata</type>\n" +
"\t<date></date>\n" +
"\t<list>\n" +
"\t\t<item date=\"2008-9-20\">工程管理待办列表(10)</item>\n" +
"\t\t<item date=\"2008-9-20\">工程管理已办列表(7)</item>\n" +
"\t</list>\n" +
"</response>";
}
}
[/code]
[code=Java]
package com.eway.wap.remote.businessinterface;
/**
* Created by IntelliJ IDEA.
* User: yuchen
* Date: 2008-11-8
* Time: 11:27:36
* To change this template use File | Settings | File Templates.
*/
public interface HttpInvokerRequestParser extends RequestParser{
/**
*
* @param _request
* @return
* @throws com.eway.wap.exception.sub.NotGetRemoteDataHttpInvokerException
*/
public String parse(String _request) ;
}
[/code]
[code=Java]
package com.eway.wap.remote.invoke.httpinvoker;
import com.eway.wap.core.context.RemoteServiceContextHolder;
import org.springframework.remoting.httpinvoker.HttpInvokerClientConfiguration;
import org.springframework.remoting.httpinvoker.SimpleHttpInvokerRequestExecutor;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import sun.net.www.protocol.http.HttpURLConnection;
public class EwayHttpInvokerRequestExecutor extends SimpleHttpInvokerRequestExecutor {
protected HttpURLConnection openConnection(HttpInvokerClientConfiguration config) throws IOException {
String serviceUrl = RemoteServiceContextHolder.getContext().getServiceUrl();
System.out.println("serviceUrl========"+serviceUrl);
URLConnection con = new URL(serviceUrl).openConnection();
if (!(con instanceof HttpURLConnection)) {
throw new IOException("Service URL [" + config.getServiceUrl() + "] is not an HTTP URL");
}
return (HttpURLConnection) con;
}
}
[/code]
大体就是这样。。。
现在遇到的问题。。就是考虑到,加入服务器段出现故障(服务器停了,电源故障,====),反正是客户段不能正常的访问了,,那么客户段回一直等待,,,为了避免长时间的等待,,不知道怎么设置。。。httpinvoker如何处理超时问题。。。。
交流也可以,我的一个java的Q..Q 群号:26572256
不知道httpinvoker对服务的超时问题可以在什么地方配置不?
为了大家了解我配置的httpinvoker。把相关的代码贴出来。。。
首先是服务器端:
web.xml
[code=Java]
<servlet>
<servlet-name>remote</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
[/code]
remote-servlet.xml
[code=Java]
</bean>
<bean name="/requestXmlData" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
<property name="service">
<ref bean="xml"/>
</property>
<property name="serviceInterface">
<value>com.eway.wap.remote.businessinterface.RequestParser</value>
</property>
</bean>
[/code]
spring的配置文件中
[code=Java]
<!--httpinvoker服务器端配置-->
<bean id="xml" class="com.eway.wap.remote.businessinterface.RequestParserImpl"/>
[/code]
客户端:
[code=Java]
<!--httpinvoker调用远程服务-->
<bean id="httpInvokerRequestParser"
class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
<property name="serviceUrl" value="eway"/>
<property name="httpInvokerRequestExecutor" ref="httpInvokerRequestExecutor"/>
<property name="serviceInterface" value="com.eway.wap.remote.businessinterface.RequestParser"/>
</bean>
<bean id="httpInvokerRequestExecutor" class="com.eway.wap.remote.invoke.httpinvoker.EwayHttpInvokerRequestExecutor"/>
[/code]
------------------------------
附录:
使用到的java接口和类如下:
com.eway.wap.remote.businessinterface.RequestParser
[code=Java]
package com.eway.wap.remote.businessinterface;
import com.eway.wap.exception.sub.NotGetRemoteDataHttpInvokerException;
/**
* Created by IntelliJ IDEA.
* User: yuchen
* Date: 2008-9-24
* Time: 10:17:17
* To change this template use File | Settings | File Templates.
*/
public interface RequestParser {
/**
*
* @param _request
* @return
* @throws NotGetRemoteDataHttpInvokerException
*/
public String parse(String _request) ;
}
[/code]
[code=Java]
package com.eway.wap.remote.businessinterface;
import com.eway.wap.exception.sub.NotGetRemoteDataHttpInvokerException;
import com.eway.wap.remote.axis.AxisJavaCallDotNet;
/**
* Created by IntelliJ IDEA.
* User: yuche
* Date: 2008-9-24
* Time: 10:40:19
* To change this template use File | Settings | File Templates.
*/
public class RequestParserImpl implements RequestParser {
public String parse(String requestXml) {
System.out.println("***************************************requestXml:"+requestXml);
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<response>\n" +
"\t<sysid>0001</sysid>\n" +
"\t<sysname></sysname>\n" +
"\t<user>123123</user>\n" +
"\t<type>wapdata</type>\n" +
"\t<date></date>\n" +
"\t<list>\n" +
"\t\t<item date=\"2008-9-20\">工程管理待办列表(10)</item>\n" +
"\t\t<item date=\"2008-9-20\">工程管理已办列表(7)</item>\n" +
"\t</list>\n" +
"</response>";
}
}
[/code]
[code=Java]
package com.eway.wap.remote.businessinterface;
/**
* Created by IntelliJ IDEA.
* User: yuchen
* Date: 2008-11-8
* Time: 11:27:36
* To change this template use File | Settings | File Templates.
*/
public interface HttpInvokerRequestParser extends RequestParser{
/**
*
* @param _request
* @return
* @throws com.eway.wap.exception.sub.NotGetRemoteDataHttpInvokerException
*/
public String parse(String _request) ;
}
[/code]
[code=Java]
package com.eway.wap.remote.invoke.httpinvoker;
import com.eway.wap.core.context.RemoteServiceContextHolder;
import org.springframework.remoting.httpinvoker.HttpInvokerClientConfiguration;
import org.springframework.remoting.httpinvoker.SimpleHttpInvokerRequestExecutor;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import sun.net.www.protocol.http.HttpURLConnection;
public class EwayHttpInvokerRequestExecutor extends SimpleHttpInvokerRequestExecutor {
protected HttpURLConnection openConnection(HttpInvokerClientConfiguration config) throws IOException {
String serviceUrl = RemoteServiceContextHolder.getContext().getServiceUrl();
System.out.println("serviceUrl========"+serviceUrl);
URLConnection con = new URL(serviceUrl).openConnection();
if (!(con instanceof HttpURLConnection)) {
throw new IOException("Service URL [" + config.getServiceUrl() + "] is not an HTTP URL");
}
return (HttpURLConnection) con;
}
}
[/code]
大体就是这样。。。
现在遇到的问题。。就是考虑到,加入服务器段出现故障(服务器停了,电源故障,====),反正是客户段不能正常的访问了,,那么客户段回一直等待,,,为了避免长时间的等待,,不知道怎么设置。。。httpinvoker如何处理超时问题。。。。
交流也可以,我的一个java的Q..Q 群号:26572256