WildFly JavaEE - 在运行时启动/停止消息驱动 Bean
WildFly JavaEE - Start / Stop Message Driven Bean at runtime
我有一个关于消息驱动 Bean (MDB) 的问题。有没有办法只在运行时生成这些?我想在我的后端提供一种方法来开始或停止接收消息。它应该通过数据库中的配置条目来控制。在启动WildFly的时候,也要先检查MDB是否可以启动。
没有 MDB 并手动创建侦听器是否符合 JavaEE 标准?
我目前正在使用以下代码
@MessageDriven(name = "MyMDB", activationConfig = {
@ActivationConfigProperty(propertyName = "maxSession", propertyValue = "2"),
@ActivationConfigProperty(propertyName = "destination", propertyValue = "java:/jms/queue/Test"),
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
@ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge")})
public class JmsConsumer implements MessageListener {
@Override
public void onMessage(final Message msg) {
if (msg instanceof TextMessage) {
try {
final String text = ((TextMessage) msg).getText();
System.out.println("message: " + text + " (" + msg.getJMSRedelivered() + ")");
} catch (final JMSException e) {
e.printStackTrace();
}
}
}
}
此代码是否也符合 Java EE?
@Singleton
@LocalBean
public class QueueWorkerManager {
private InitialContext initialContext = null;
private QueueConnectionFactory queueConnectionFactory = null;
private Queue queue = null;
private QueueConnection queueConnection = null;
private QueueSession queueSession = null;
private MessageConsumer consumer = null;
@PostConstruct
public void init() {
try {
this.initialContext = new InitialContext();
this.queueConnectionFactory = (QueueConnectionFactory) initialContext
.lookup("java:/ConnectionFactory");
this.queue = (Queue) initialContext.lookup(MyQueueSender.WORKER_QUEUE);
this.queueConnection = queueConnectionFactory.createQueueConnection();
this.queueSession = queueConnection.createQueueSession(false, Session.CLIENT_ACKNOWLEDGE);
this.consumer = queueSession.createConsumer(this.queue);
this.consumer.setMessageListener(new ConsumerMessageListener());
this.queueConnection.start();
} catch (Exception ex) {
ex.printStackTrace();
}
}
@PreDestroy
public void destroy() {
this.stopConsumer(this.consumer;
if(this.consumer != null) {
try {
this.consumer.close();
} catch (JMSException e) {
}
this.consumer = null;
}
if(this.queueSession != null) {
try {
this.queueSession.close();
} catch (JMSException e) {
}
this.queueSession = null;
}
if(this.queueConnection != null) {
try {
this.queueConnection.close();
} catch (JMSException e) {
}
this.queueConnection = null;
}
}
}
public class ConsumerMessageListener implements MessageListener {
@Override
public void onMessage(Message message) {
TextMessage textMessage = (TextMessage) message;
try {
System.out.println("message: " + textMessage.getText() + " (" + msg.getJMSRedelivered() + ")");
message.acknowledge();
} catch (JMSException | InterruptedException e) {
e.printStackTrace();
}
}
}
我建议你
- 向您的 MDB 添加
@DeliveryActive(
false/true) 注释,以便以所需的初始状态启动它
- 使用 JMX 或 JBoss CLI 以编程方式更改 MDB
active
属性的值
一切都在这里解释:
https://docs.wildfly.org/16/Developer_Guide.html#Message_Driven_Beans_Controlled_Delivery
祝你好运
我有一个关于消息驱动 Bean (MDB) 的问题。有没有办法只在运行时生成这些?我想在我的后端提供一种方法来开始或停止接收消息。它应该通过数据库中的配置条目来控制。在启动WildFly的时候,也要先检查MDB是否可以启动。
没有 MDB 并手动创建侦听器是否符合 JavaEE 标准?
我目前正在使用以下代码
@MessageDriven(name = "MyMDB", activationConfig = {
@ActivationConfigProperty(propertyName = "maxSession", propertyValue = "2"),
@ActivationConfigProperty(propertyName = "destination", propertyValue = "java:/jms/queue/Test"),
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
@ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge")})
public class JmsConsumer implements MessageListener {
@Override
public void onMessage(final Message msg) {
if (msg instanceof TextMessage) {
try {
final String text = ((TextMessage) msg).getText();
System.out.println("message: " + text + " (" + msg.getJMSRedelivered() + ")");
} catch (final JMSException e) {
e.printStackTrace();
}
}
}
}
此代码是否也符合 Java EE?
@Singleton
@LocalBean
public class QueueWorkerManager {
private InitialContext initialContext = null;
private QueueConnectionFactory queueConnectionFactory = null;
private Queue queue = null;
private QueueConnection queueConnection = null;
private QueueSession queueSession = null;
private MessageConsumer consumer = null;
@PostConstruct
public void init() {
try {
this.initialContext = new InitialContext();
this.queueConnectionFactory = (QueueConnectionFactory) initialContext
.lookup("java:/ConnectionFactory");
this.queue = (Queue) initialContext.lookup(MyQueueSender.WORKER_QUEUE);
this.queueConnection = queueConnectionFactory.createQueueConnection();
this.queueSession = queueConnection.createQueueSession(false, Session.CLIENT_ACKNOWLEDGE);
this.consumer = queueSession.createConsumer(this.queue);
this.consumer.setMessageListener(new ConsumerMessageListener());
this.queueConnection.start();
} catch (Exception ex) {
ex.printStackTrace();
}
}
@PreDestroy
public void destroy() {
this.stopConsumer(this.consumer;
if(this.consumer != null) {
try {
this.consumer.close();
} catch (JMSException e) {
}
this.consumer = null;
}
if(this.queueSession != null) {
try {
this.queueSession.close();
} catch (JMSException e) {
}
this.queueSession = null;
}
if(this.queueConnection != null) {
try {
this.queueConnection.close();
} catch (JMSException e) {
}
this.queueConnection = null;
}
}
}
public class ConsumerMessageListener implements MessageListener {
@Override
public void onMessage(Message message) {
TextMessage textMessage = (TextMessage) message;
try {
System.out.println("message: " + textMessage.getText() + " (" + msg.getJMSRedelivered() + ")");
message.acknowledge();
} catch (JMSException | InterruptedException e) {
e.printStackTrace();
}
}
}
我建议你
- 向您的 MDB 添加
@DeliveryActive(
false/true) 注释,以便以所需的初始状态启动它 - 使用 JMX 或 JBoss CLI 以编程方式更改 MDB
active
属性的值
一切都在这里解释: https://docs.wildfly.org/16/Developer_Guide.html#Message_Driven_Beans_Controlled_Delivery
祝你好运