osgi9——camel动态路由

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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值