作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我们正在编写 3 个消费者,用于使用 Apache Camel 和 Spring
使用 Activemq
队列名称作为 ThermalMap
消费消息。
我是 Spring
的新手,我尝试从 bean.xml
获取特定的 beanId("amqLinkId") 值。为此,我编写了以下 3 个文件。
变量声明.java
public class VariablesDeclarations {
private String amqLink;
public String getAmqLink() {
return amqLink;
}
public void setAmqLink(String amqLink) {
this.amqLink = amqLink;
}
}
FirstConsumer.java
import org.apache.camel.CamelContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class FirstConsumer {
public static void main(String[] args) {
ApplicationContext contextObject=new ClassPathXmlApplicationContext("bean.xml");
//Forwarding cursor to ConsumersMiddileWare class using CamelContext
CamelContext camelObject=contextObject.getBean("activeContext", CamelContext.class);
System.out.println("H");
}
}
ConsumersMiddileware.java
import org.apache.camel.builder.RouteBuilder;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ConsumersMiddileWare extends RouteBuilder{
@Override
public void configure() throws Exception {
System.out.println("Bye");
BeanFactory beanFactory=new ClassPathXmlApplicationContext(new String[]{"bean.xml"});
VariablesDeclarations vd=(VariablesDeclarations) beanFactory.getBean("amqLinkId");
System.out.println(vd.getAmqLink());
}
}
bean.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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
http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core-5.5.0.xsd
">
<!-- Creating camelContext element for forwarding to another ConsumersMiddileware class -->
<camelContext id="activeContext" xmlns="http://camel.apache.org/schema/spring">
<routeBuilder ref="consumerMiddileWareContext" />
</camelContext>
<bean id="consumerMiddileWareContext" class="ActivemqPackage.ConsumersMiddileWare"></bean>
<!-- Following bean created for getting variable values into classes-->
<bean id="amqLinkId" class="ActivemqPackage.VariablesDeclarations">
<property name="amqLink" value="activemq:queue:ThermalMap"></property>
</bean>
</beans>
工作流程:
如果您运行FirstConsumer.java
,它应该转发到ConsumersMiddileWare.java
。我想从找到
并分配给任何 amqLinkId
值bean.xmlString
对象。
如果我找到了amqLinkId
值,稍后我想直接在ConsumersMiddileWare.java
中编写以下代码
String activeMqURI=vd.getAmqLink();
from( activeMqURI).to("bean:activemqProcessor?method=processMessage");
问题:
如果我触发FirsrConsumer.java
,它会转发到ConsumersMiddileWare.java
。它会继续打印Bye
而不是打印amqLinkId
值如下所示。
Bye
Bye
Bye
Bye
.
.
谁能解释清楚。
谢谢
最佳答案
您可以使用 SpringRouteBuilder 来代替 RouteBuilder。这个类有一些额外的方法来从spring应用程序上下文(例如spring xml文件)获取bean
public class ConsumersMiddileWare extends SpringRouteBuilder{
@Override
public void configure() throws Exception {
VariablesDeclarations vd = lookup("amqLinkId", VariablesDeclarations.class)'
}
替代方法是在常规 RouteBuilder 中使用camelContext.getRegistry
public class ConsumersMiddileWare extends RouteBuilder{
@Override
public void configure() throws Exception {
VariablesDeclarations vd = context().getRegistry().lookup("amqLinkId", VariablesDeclarations.class)'
}
关于java - 如何从 bean.xml 获取 bean 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18931077/
我是一名优秀的程序员,十分优秀!