
Apache Camel的Header、Property、Body配置示例
<?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:camel="http://camel.apache.org/schema/spring"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
<bean id="echo" class="com.lala.bean.Echo"></bean>
<bean id="bean1" class="com.lala.bean.Bean1"></bean>
<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
<route autoStartup="true">
<from uri="timer://aaa?fixedRate=true&period=60000" />
<process ref="bean1"/>
<!-- 设置所有的headers(会去掉之前的所有header) -->
<setOutHeader headerName="name">
<constant>白展堂</constant>
</setOutHeader>
<!-- 添加一个header -->
<setHeader headerName="id">
<constant>100010</constant>
</setHeader>
<!-- 设置property -->
<setProperty propertyName="type">
<simple>${in.header.id}</simple>
</setProperty>
<!-- 设置body -->
<setBody>
<simple>name:${in.header.name},type:${property.type}</simple>
</setBody>
<process ref="echo"/>
</route>
</camelContext>
</beans>
1。运行VirutalBox(VirtualBox.exe),不要运行虚拟机;2。打开寄主机的“网络连接”页面,记下“virtualbox host-only network”的网络设置(我的默认设置为(安装完VirtualBox后,网络连接就自动多了个VirtualBox Host-Only Network):IP地址为192.168.56.1,掩码为255.255.255.0);3。启动虚拟机,将虚拟机的网络设置如下:IP地址:192.168.56.2掩码:255.255.255.0网关:192.168.56.1(我的试验发现,不需要设置DNS);4。尝试宿主机与虚拟机互ping如果发现ping不通,主要的可能原因是防火墙拦截了互ping时,可以将2台计算机的防火墙先关闭,证明互ping成功后再打开防火墙5。我的目标是宿主机连接使用虚拟机的数据库服务,SQLServer的默认端口为:1433,因此,对虚拟机 Windows XP防火墙设置例外端口,使得宿主机可连接虚拟机,对虚拟机 Windows XP设置如下:开始-->控制面板-->Windows防火墙-->点击“例外”选项卡-->点击“添加端口”按钮起一个容易记的名字,端口为:1433,单选按钮:TCP,点确定。宿主机测试是否能连接:在Windows 7WIN+R-->cmd-->telnet 192.168.68.2 1433出现新的命令窗口,命令窗口的标题是“telnet 192.168.68.2”,就证明成功了。如果告诉你telnet不是内部命令,依次打开“开始”→“控制面板”→“打开或关闭Windows功能”,在打开的窗口处,寻找并勾选“Telnet客户端”,然后点击“确定”。顺利安装后,再在运行下输入此命令就OK了。 通过Host-Only使宿主机与虚拟机通信,此时虚拟机无法访问外网。要解决该问题,最简单的方法是:为虚拟机再设置一块网卡,连接方式为NAT 另外,如果有路由器,则通过桥接方式(Bridge)就可以实现既互相通信又能上网了,因为此时宿主机与虚拟机是局域网上两台平等的计算机。 在项目开发过程中,我想大部分系统都需要对接另外的系统。对接方式有很多种,现在最常见的就是https请求了。现将Java发送https请求的工具类整理一下,有需要用到的同学拿走不谢。
该方法使用apache的httpclient实现
第一步:创建SSLClient
import java.security.cert.CertificateException
import java.security.cert.X509Certificate
import javax.net.ssl.SSLContext
import javax.net.ssl.TrustManager
import javax.net.ssl.X509TrustManager
import org.apache.http.conn.ClientConnectionManager
import org.apache.http.conn.scheme.Scheme
import org.apache.http.conn.scheme.SchemeRegistry
import org.apache.http.conn.ssl.SSLSocketFactory
import org.apache.http.impl.client.DefaultHttpClient
public class SSLClient extends DefaultHttpClient{
public SSLClient() throws Exception{
super()
SSLContext ctx = SSLContext.getInstance("TLS")
X509TrustManager tm = new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null
}
}
ctx.init(null, new TrustManager[]{tm}, null)
SSLSocketFactory ssf = new SSLSocketFactory(ctx,SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER)
ClientConnectionManager ccm = this.getConnectionManager()
SchemeRegistry sr = ccm.getSchemeRegistry()
sr.register(new Scheme("https", 443, ssf))
}
}
第二步:实现自己的https工具类
import org.apache.http.HttpEntity
import org.apache.http.HttpResponse
import org.apache.http.client.HttpClient
import org.apache.http.client.methods.HttpGet
import org.apache.http.client.methods.HttpPost
import org.apache.http.entity.StringEntity
import org.apache.http.message.BasicHeader
import org.apache.http.protocol.HTTP
import org.apache.http.util.EntityUtils
import java.util.Map
public class HttpsClientUtil {
private static final String CHAREST = "utf-8"
/**
* 发送post请求
* @param url
* @return
*/
public static String doPost(String url,String mapParam){
HttpClient httpClient = null
HttpPost httpPost = null
String result = null
try{
httpClient = new SSLClient()
httpPost = new HttpPost(url)
//设置参数
httpPost.setHeader("Content-Type", "application/json")
httpPost.addHeader("Authorization", "Basic YWRtaW46")
StringEntity s = new StringEntity(mapParam, CHAREST)
s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json"))
httpPost.setEntity(s)
HttpResponse response = httpClient.execute(httpPost)
if(response != null){
HttpEntity resEntity = response.getEntity()
if(resEntity != null){
result = EntityUtils.toString(resEntity,CHAREST)
}
}
}catch(Exception ex){
ex.printStackTrace()
}
return result
}
/**
* 发送get请求
* @param url 链接地址
* @return
*/
public static String doGet(String url){
HttpClient httpClient = null
HttpGet httpGet= null
String result = null
try {
httpClient = new SSLClient()
httpGet = new HttpGet(url)
HttpResponse response = httpClient.execute(httpGet)
if(response != null){
HttpEntity resEntity = response.getEntity()
if(resEntity != null){
result = EntityUtils.toString(resEntity,CHAREST)
}
}
} catch (Exception e) {
e.printStackTrace()
}
return result
}
/**
* 发送get请求,并设置get的请求头
* @param url 链接地址
* @return
*/
public static String setHeadDoGet(String url,Map headers){
HttpClient httpClient = null
HttpGet httpGet= null
String result = null
try {
httpClient = new SSLClient()
httpGet = new HttpGet(url)
for (Map.Entry e : headers.entrySet()) {
httpGet.addHeader(e.getKey(), e.getValue())
}
HttpResponse response = httpClient.execute(httpGet)
if(response != null){
HttpEntity resEntity = response.getEntity()
if(resEntity != null){
result = EntityUtils.toString(resEntity,CHAREST)
}
}
} catch (Exception e) {
e.printStackTrace()
}
return result
}
}
ps:有需要设置post请求头的请求,同学们可以参考setHeadDoGet方法进行编写。 需要用到的apache的httpclient-4.5.9.jar包 。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)