无法将JMS资源与Openliberty绑定。

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

Unable to bind JMS resources with Openliberty

问题

以下是您要翻译的内容:

我正在使用OpenLiberty上的Jakarta Messaging迈出第一步。一个简单的发送方和接收方"test"。但是我在绑定JNDI资源方面遇到了困难。

这是我的 server.xml 配置:

...
    <featureManager>
        <feature>webProfile-9.1</feature>
        <feature>mail-2.0</feature>
        <feature>concurrent-2.0</feature>
        <feature>messaging-3.0</feature>
    </featureManager>
...
    <messagingEngine>
        <queue id="SIMPLE_QUEUE"/>
    </messagingEngine>

    <jmsQueueConnectionFactory jndiName="jms/JmsFactory">
        <properties.wasJms />
    </jmsQueueConnectionFactory>

    <jmsQueue id="jms/JmsQueue" jndiName="jms/JmsQueue">
        <properties.wasJms queueName="SIMPLE_QUEUE"/>
    </jmsQueue>

    <jmsActivationSpec id="jms/Reader">
        <properties.wasJms destinationRef="jms/JmsQueue"/>
    </jmsActivationSpec>
...

这是我的发送方

import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import jakarta.faces.view.ViewScoped;
import jakarta.inject.Named;
import jakarta.jms.*;

import java.io.Serializable;

@Named
@ViewScoped
public class Sender implements Serializable {
    @Resource(lookup = "jms/JmsQueue")
    private Queue queue;

    @Resource(lookup = "jms/JmsFactory")
    private ConnectionFactory connectionFactory;

    private Connection connection;
    private Session session;
    private MessageProducer producer;

    @PostConstruct
    public void initialize() {
        try {
            connection = connectionFactory.createConnection();
            session = connection.createSession();
            producer = session.createProducer(queue);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void send() {
        try {
            TextMessage message = session.createTextMessage();
            message.setText("Hello world");
            producer.send(message);
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}

然而,connection = connectionFactory.createConnection(); 这一行会抛出一个空指针异常。我尝试过 打印JNDI绑定,但JMS相关的内容都不见了。

对于我做错了什么有任何想法吗?

编辑:

根据@Gas的提示,添加了 messagingServer-3.0messagingClient-3.0 功能解决了问题,JMS组件出现在JNDI中。

英文:

I am doing my first steps with Jakarta Messaging on OpenLiberty. A simple sender and receiver "test". But I am having troubles binding the JNDI resources.

This is my server.xml config:

...
    &lt;featureManager&gt;
        &lt;feature&gt;webProfile-9.1&lt;/feature&gt;
        &lt;feature&gt;mail-2.0&lt;/feature&gt;
        &lt;feature&gt;concurrent-2.0&lt;/feature&gt;
        &lt;feature&gt;messaging-3.0&lt;/feature&gt;
    &lt;/featureManager&gt;
...
    &lt;messagingEngine&gt;
        &lt;queue id=&quot;SIMPLE_QUEUE&quot;/&gt;
    &lt;/messagingEngine&gt;

    &lt;jmsQueueConnectionFactory jndiName=&quot;jms/JmsFactory&quot;&gt;
        &lt;properties.wasJms /&gt;
    &lt;/jmsQueueConnectionFactory&gt;

    &lt;jmsQueue id=&quot;jms/JmsQueue&quot; jndiName=&quot;jms/JmsQueue&quot;&gt;
        &lt;properties.wasJms queueName=&quot;SIMPLE_QUEUE&quot;/&gt;
    &lt;/jmsQueue&gt;

    &lt;jmsActivationSpec id=&quot;jms/Reader&quot;&gt;
        &lt;properties.wasJms destinationRef=&quot;jms/JmsQueue&quot;/&gt;
    &lt;/jmsActivationSpec&gt;
...

This is my sender

import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import jakarta.faces.view.ViewScoped;
import jakarta.inject.Named;
import jakarta.jms.*;

import java.io.Serializable;

@Named
@ViewScoped
public class Sender implements Serializable {
    @Resource(lookup = &quot;jms/JmsQueue&quot;)
    private Queue queue;

    @Resource(lookup = &quot;jms/JmsFactory&quot;)
    private ConnectionFactory connectionFactory;

    private Connection connection;
    private Session session;
    private MessageProducer producer;

    @PostConstruct
    public void initialize() {
        try {
            connection = connectionFactory.createConnection();
            session = connection.createSession();
            producer = session.createProducer(queue);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void send() {
        try {
            TextMessage message = session.createTextMessage();
            message.setText(&quot;Hello world&quot;);
            producer.send(message);
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}

However the line connection = connectionFactory.createConnection(); throws a nullpointer exception. I tried printing the JNDI bindings, but the JMS stuff is missing.

Any ideas on what I am doing wrong?

Edit:

Due to the tip from @Gas, adding &lt;feature&gt;messagingServer-3.0&lt;/feature&gt; and &lt;feature&gt;messagingClient-3.0&lt;/feature&gt; solved the issue and the JMS components appear in JNDI.

答案1

得分: 2

因为 @Gas 的提示,添加以下内容解决了问题,并且 JMS 组件出现在 JNDI 中:

...
    <feature>messagingServer-3.0</feature>
    <feature>messagingClient-3.0</feature>
...
英文:

Due to the tip of @Gas, adding

...
    &lt;feature&gt;messagingServer-3.0&lt;/feature&gt;
    &lt;feature&gt;messagingClient-3.0&lt;/feature&gt; 
...

solved the issue and the JMS components appear in JNDI.

huangapple
  • 本文由 发表于 2023年4月17日 21:24:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76035667.html
匿名

发表评论

匿名网友

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

确定