osgi7——camel發送rabbitmq

esb環境下經常需要用時間處理,下面介紹用camel發送rabbitmq。

1.blueprint的配置

<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0"
           xsi:schemaLocation="
http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0 http://aries.apache.org/schemas/blueprint-cm/blueprint-cm-1.1.0.xsd"
>
     <cm:property-placeholder persistent-id="con.yyc.rabbitMQ" update-strategy="reload">
        <cm:default-properties>
            <cm:property name="RabbitMQ.password" value="dsfdss23i55423493/fjdsfdsklou545" />
            <cm:property name="RabbitMQ.host" value="192.168.1.43" />
            <cm:property name="RabbitMQ.port" value="5672"/>
            <cm:property name="RabbitMQ.username" value="guest" />
            <cm:property name="RabbitMQ.password" value="guest" />
            
            
            <cm:property name="queue_name" value="con.yyc.rabbitMQ.loginlog" />
            <cm:property name="exchang_name" value="con.yyc.rabbitMQ.loginlog" />
            
        </cm:default-properties>
    </cm:property-placeholder>
  

     <bean id="customConnectionFactory" class="com.rabbitmq.client.ConnectionFactory">
        <property name="host" value="${RabbitMQ.host}"/>
        <property name="port" value="${RabbitMQ.port}"/>
        <property name="username" value="${RabbitMQ.username}"/>
        <property name="password" value="${RabbitMQ.password}"/>
    </bean>

    <bean id="LoginSenderBean" class="con.yyc.rabbitMQ.LoginSenderProcessor">
        <property name="camelcontext" ref="camel-ssoweb"/>
    </bean>
	
    <camelContext id="camel-ssoweb" xmlns="http://camel.apache.org/schema/blueprint" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd">
        <endpoint id="rabbitMQ" uri="rabbitmq://{{RabbitMQ.host}}:{{RabbitMQ.port}}/{{exchang_name}}?queue={{queue_name}}&connectionFactory=#customConnectionFactory&durable=true&autoDelete=false"/>
        <route>
            <from  uri="vm:_LoginSender"  />
            <to   ref="rabbitMQ" />
        </route>
    </camelContext>
   
</blueprint>

2.LoginSendProcessor.java

package com.yyc.rabbimq;

import org.apache.camel.CamelContext;
import org.apache.camel.EndpointInject;
import org.apache.camel.Exchange;
import org.apache.camel.ExchangePattern;
import org.apache.camel.ProducerTemplate;

import org.slf4j.LoggerFactory;

public class LoginSenderProcessor {

    private static final org.slf4j.Logger log = LoggerFactory.getLogger(LoginSenderProcessor.class);

    private CamelContext camelcontext;

    public void setCamelcontext(CamelContext camelcontext) {
        this.camelcontext = camelcontext;
    }

    @EndpointInject(uri = "vm:_LoginSender")
    private ProducerTemplate producer;

    @Override
    public void invoke(String message) {
        try {
            Exchange exchg = camelcontext.getEndpoint("vm:_LoginSender").createExchange();
            exchg.setPattern(ExchangePattern.InOut);
            exchg.getIn().setHeader(Exchange.CONTENT_TYPE, "application/x-www-form-urlencoded;charset=UTF-8");
            exchg.getIn().setBody(message);
            producer.send(exchg);
        } catch (Exception e) {
            log.info("LoginSenderProcessor---invoke:" + e);
        }

    }

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章