消息驱动的 Bean 读取同一消息的两次



早上好,在我的时区。

应用程序服务器 -> WAS 7EJB 3.0

在我正在从事的项目中,我们使用消息驱动的 Bean 从队列中读取消息。此消息驱动的 Bean 读取同一消息两次,在第二次读取时,由于数据库插入中的完整性约束,它会引发异常。为什么这个消息驱动的 Bean 会读取两次消息。我们在队列中只使用一个侦听器,并且只有一个 MDB 附加到该侦听器。我们通过注释使用以下激活配置属性 1 条消息选择器 2 目的地类型 3 目的地

代码片段

@MessageDriven(activationConfig = {
    @ActivationConfigProperty(propertyName = "messageSelector", propertyValue = "ResponseType = 'XXXXX'"),
    @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
    @ActivationConfigProperty(propertyName = "destination", propertyValue = "jms/YYYY")})

提前致谢此致敬意

@MessageDriven(activationConfig = {
    @ActivationConfigProperty(propertyName = "destinationLookup", propertyValue = "java:/jms/queue/data"),
    @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
    @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge") })

使用此配置来指定消息的确认,我认为我们还需要指定 destinationLookupdestination 属性来指定严格的点对点通信。

使用消息侦听器验证接收消息的确切时间以及发布消息的生存时间

@Stateless
public class MessageSender {
    @Inject
    JMSContext jmsContext;
    public void sendMessage(String message, Destination destination) {
        JMSProducer messageProducer = jmsContext.createProducer().setAsync(
                new CompletionListener() {
                    public void onException(Message message, Exception exception) {
                        System.out
                                .println("Message not delivered!!!!nAn exception has occured "
                                        + exception);
                    }
                    public void onCompletion(Message message) {
                        System.out
                                .println("Message  delivered : hooah ");
                    }
                });
        // To check if both the messages are getting expired after the mentioned time
        messageProducer.setTimeToLive(6000);
        messageProducer.send(destination, message);
    }
}

最新更新