osgi9——camel动态路由

来源:互联网 发布:设计餐厅菜单软件 编辑:程序博客网 时间:2024/05/16 08:53

1.camel路由uri都是静态的,一般容器启动的时候,它会去预执行。但是有些时候需要动态的去执行一些uri,好比mq,mq有exchange和queue,这两项有些时候是动态的办法,用普通的配置是不行的,所以我们解决的时候就需要动态路由

请看下面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"xmlns:camel-cxf="http://camel.apache.org/schema/blueprint/cxf"xmlns:cxfcore="http://cxf.apache.org/blueprint/core"xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd                                  http://camel.apache.org/schema/blueprint/cxf http://camel.apache.org/schema/blueprint/cxf/camel-cxf.xsd                                http://cxf.apache.org/blueprint/core http://cxf.apache.org/schemas/blueprint/core.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="yyc.com.MQ"update-strategy="reload"><cm:default-properties><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: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="dynamicRouter" class="yyc.com.camel.DynamicRouter"><property name="host" value="${RabbitMQ.host}" /><property name="port" value="${RabbitMQ.port}" /></bean><camelContext id="RabbitmqCamelContext"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"><!--动态的发送mq --><route><from uri="direct-vm:toRabbitmq" /><dynamicRouter><method ref="dynamicRouter" method="dynamicToRabbitmq" /></dynamicRouter></route></camelContext></blueprint>
yyc.com.camel.DynamicRouter  实现
import java.util.Map;import org.apache.camel.Headers;import org.apache.camel.Properties;import org.slf4j.Logger;import org.slf4j.LoggerFactory;/** * * @author yyc */public class DynamicRouter {    private static final Logger log = LoggerFactory.getLogger(DynamicRouter.class);        private String host;        private String port;            public String dynamicToRabbitmq(@Headers Map<String, Object> headers, @Properties Map<String, Object> properties) {        int invoked = 0;        Object current = properties.get("invoked");        if (current != null) {            invoked = Integer.valueOf(current.toString());        }        invoked++;        properties.put("invoked", invoked);                log.info("调用动态的mq:"+properties);        if (invoked == 1  && properties.get("queue")!=null && properties.get("exchange")!=null) {            String queue = (String) properties.get("queue");            String exchange = (String) properties.get("exchange");            String routingKey = (String) properties.get("routingKey");            String result = "rabbitmq://"+host+":"+port+"/"+exchange+"?connectionFactory=#customConnectionFactory&durable=true&autoDelete=false&exchangeType=topic&queue=" + queue + "&routingKey=" + routingKey;                      log.info("发送mq到"+exchange);            return result;        }                return null;    }    public void setHost(String host) {        this.host = host;    }    public void setPort(String port) {        this.port = port;    }    }

这样的话就能实现动态的发送mq消息

0 0
原创粉丝点击