我在Spring做一些关于JMS的练习,我有一些疑问 .
在练习的解决方案中,我有一个名为 JmsInfrastructureConfig 的配置类:
package config;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class JmsInfrastructureConfig {
/**
* Create a ConnectionFactory using ActiveMQ:
*/
@Bean
public ConnectionFactory connectionFactory(){
return new ActiveMQConnectionFactory("vm://embedded?broker.persistent=false");
}
/**
* Create a Queue for Dining objects using ActiveMQ:
*/
@Bean
public Destination diningQueue() {
return new ActiveMQQueue("rewards.queue.dining");
}
/**
* Create a Queue for Confirmation objects using ActiveMQ:
*/
@Bean
public Destination confirmationQueue() {
return new ActiveMQQueue("rewards.queue.confirmation");
}
}
我知道在基于JMS的应用程序中, Connection 对象是从 ConnectionFactory 获得的 .
根据我的理解阅读文档, Connection 是 JMS core components 之一,它用于将消息放入queue \ topic并使用来自queue \ topic的消息 . 这个断言是真的还是这是一个错误的断言?
JMS Connection 是从 factory 对象获得的,因此在之前的配置类中声明了这个创建并返回特定工厂的bean:
@Bean
public ConnectionFactory connectionFactory(){
return new ActiveMQConnectionFactory("vm://embedded?broker.persistent=false");
}
这对我来说很清楚但在这里我有第一个疑问 . 它创建一个新的 ActiveMQConnectionFactory 对象,因为它被 Apache Active MQ 用作 Message Oriented Middleware .
Apache Active MQ 究竟做了什么(和 MOM 一般)?它处理队列和主题以及对这些对象的访问?) .
到底是什么 vm://embedded?broker.persistent=false ?它是经纪人URL吗?我究竟如何解释这个地址?
然后进入 JmsInfrastructureConfig 类创建这两个bean:
// Create a Queue for Dining objects using ActiveMQ:
@Bean
public Destination diningQueue() {
return new ActiveMQQueue("rewards.queue.dining");
}
// Create a Queue for Confirmation objects using ActiveMQ:
@Bean
public Destination confirmationQueue() {
return new ActiveMQQueue("rewards.queue.confirmation");
}
两者都创建了一个特定的 queue ,名字各自: rewards.queue.dining 和 rewards.queue.confirmation (我认为这些是队列名称,还是没有?)
因此,这些bean创建了一个队列, 生产环境 者可以在其中放置消息,消费者可以阅读消息 .
这些方法返回一个 Destination 对象,究竟是什么代表了这个对象?它是访问队列的网关(访问意味着写入队列中的读取)吗?或者是什么?
另一个疑问是:为什么在这个应用程序的acrhitecture中,返回 ConnectionFactory 的bean的声明是在同一个类中定义创建队列的bean?
1 回答
是的 .
这是您的brokerURL,这就是您应该如何解释它:
vm - 这是您的协议,意味着虚拟机(因为您使用的是嵌入式代理,在您自己的应用程序中运行)
embedded - 这是你的brokerName,所以现在你有一个名为"embedded"的嵌入式代理(有点令人困惑)
?broker.persistent = false - 这只是您的经纪人的参数 . 这是一个可选参数,如果您愿意,可以附加更多参数
是的,这些是你的队列 .
这是您可以用来访问队列的对象 . 通过使用它,您可以生成(发送)或使用(读取)消息 .
他们不一定在同一个 class . 然而,该示例旨在尽可能简单,因此它可以在现场为您创建这些队列,因此您可以使用它 .
我认为回答的问题太过广泛 . 如果你需要一个非常高级的解释,你应该首先阅读一些内容,如下所示:Message broker