【发布时间】:2021-10-09 04:40:43
【问题描述】:
我有以下 Java 进程正在侦听 ActiveMQ 的 MyMessageQueue 队列。我可以看到使用此语句 System.out.println("Message Retrieved is:" +message); 检索到的消息
在processBrokerQueues 方法内。
@Component
public class MessageConsumer {
@Autowired
private JavaMailSender javaMailSender;
// one instance, reuse
private final CloseableHttpClient httpClient = HttpClients.createDefault();
private static Connection connection;
// URL of the JMS server. DEFAULT_BROKER_URL will just mean that JMS server is on localhost
private static String url = ActiveMQConnection.DEFAULT_BROKER_URL;
private static String subject = "MyMessageQueue"; //Queue Name
// default broker URL is : tcp://localhost:61616"
private static ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
private static final Logger logger = LoggerFactory.getLogger(MessageConsumer.class);
private static final DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
// Working Code with JMS 2.0
@JmsListener(destination = "MyMessageQueue")
public void processBrokerQueues(String message) throws DaoException, JMSException {
System.out.println("Message Retrieved is:" + message);
}
private void createConnection() throws JMSException {
// Getting JMS connection from the server
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
connection = connectionFactory.createConnection();
}
private void close() throws IOException {
httpClient.close();
}
}
我想知道,如果我想听 20-30 个不同的队列,添加多个具有不同队列名称的 @JmsListener(destination = "MyMessageQueue") 是否正确,例如 MessageQueue1, MessageQueue2 ....... 等等,直到 MessageQueue30?
【问题讨论】:
-
@JustinBertram 不确定。我想知道是否必须切换到主题才能做到这一点?
-
@JmsListener可以配置为队列或主题。没关系。答案只是碰巧使用了主题。 -
@JustinBertram 好的,我会测试一下。还有一个问题,在生产者方面,我可能会向 10 个或更多不同的队列发送 10 个或更多不同的消息。在这种情况下,我需要配置
@JmsListner来监听 10 个或更多队列。想问制作方也没有Topic相关要求吗?谢谢! -
我不明白你在问什么。 “生产者方面的主题相关要求”是什么意思?这是 JMS。您可以使用队列或主题。没有什么要求您使用主题。
标签: java activemq spring-jms