JMS 1.1持久订阅 – 无法设置客户端ID

huangapple go评论74阅读模式
英文:

JMS 1.1 durable subscription - can't set Client ID

问题

我正在尝试使用 JMS 1.1 设置持久订阅,但我陷入了一个进退两难的境地:

  • 如果我不设置 clientID,就会出现 "clientID cannot be null" 错误...
  • 如果我尝试设置它,就会得到:
    com.ibm.msg.client.jms.DetailedIllegalStateException: JMSCC3031: *在连接被使用后无法设置客户端 ID。
    连接的客户端 ID 只能设置一次,而且只能在连接被使用之前设置。
    在使用连接之前设置客户端 ID。

我该如何解决这个问题?我如何使连接处于 "未使用 "状态?
或者 - 如异常消息所建议的 - 我如何在使用连接之前设置 ID?

我的代码片段:

public class BbsListener implements MessageListener {

...

public BbsListener(BbsListenerConfig config) {
    try {
        Context context = new InitialContext();
        TopicConnectionFactory topicConnectionFactory = (TopicConnectionFactory) context.lookup(config.getConnectionFactoryName());
        TopicConnection topicConnection = topicConnectionFactory.createTopicConnection();
        topicConnection.setClientID("ID");
        TopicSession topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
        Topic topic = (Topic) context.lookup(config.getTopicName());
        topicSubscriber = topicSession.createDurableSubscriber(topic, "EAMPtestSubscriber");
        topicSubscriber.setMessageListener(this);
        topicConnection.start();
    }

...

谢谢

英文:

I am trying to setup a durable subscription with JMS 1.1 but I get in a Catch 22:

  • if I don't set the clientID, I get a "clientID cannot be null" error...
  • if I try to set it, I get:
    com.ibm.msg.client.jms.DetailedIllegalStateException: JMSCC3031: A client ID cannot be set after connection has been used.
    The client ID of a connection can be set only once, and only before the connection is used.
    Set the client ID before using the connection.

How do I solve this? How do I make the connection 'unused'?
Or - as the exception message suggests - how do I set the ID before I use the connection?

My code snippet:

public class BbsListener implements MessageListener {

...

public BbsListener(BbsListenerConfig config) {
    try {
        Context context = new InitialContext();
        TopicConnectionFactory topicConnectionFactory = (TopicConnectionFactory) context.lookup(config.getConnectionFactoryName());
        TopicConnection topicConnection = topicConnectionFactory.createTopicConnection();
        topicConnection.setClientID("ID");
        TopicSession topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
        Topic topic = (Topic) context.lookup(config.getTopicName());
        topicSubscriber = topicSession.createDurableSubscriber(topic, "EAMPtestSubscriber");
        topicSubscriber.setMessageListener(this);
        topicConnection.start();
    }
    
...

Thank you

答案1

得分: 0

看起来您的应用正在运行在一个Java EE应用服务器上。如果是这种情况,您需要注意使用何种类型的连接工厂以及在哪里调用setMessageListener()。首先,“出站”连接工厂适用于发送消息(因此称为“出站”)。这是JCA的一部分。其次,在EJB中不能调用setMessageListener(),因为规范不允许这样做。我建议您使用普通的JMS连接工厂,而不是应用服务器中的池化连接工厂。

英文:

It looks like your app is running on a Java EE application server. If that's the case you'll need to be careful about what kind of connection factory you use and where you invoke setMessageListener(). First, an "outbound" connection factory is meant to be used for sending messages (hence the name "outbound"). This is part of JCA. Second, you can't call setMessageListener() in an EJB as that's not allowed by spec. I recommend you just use a normal JMS connection factory rather than a pooled one from the application server.

huangapple
  • 本文由 发表于 2020年9月17日 22:51:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63940707.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定