java Redhat Fuse-Karaf拒绝注入jms连接工厂

whlutmcx  于 2023-01-04  发布在  Java
关注(0)|答案(1)|浏览(133)

熔断卡拉夫保险丝-卡拉夫-7.11.1.保险丝-7_11_1 -00013-红帽子-00003。
我正在使用amqp协议创建一个从servlet到ActiveMQ 5.9的简单桥。我设法配置了一个ConectionFactory,并使用jms:send命令测试了正常。我编写了一个JMS服务,该服务从servlet端响应POST,但无法创建连接工厂。

admin@root()〉jms:连接工厂JMS连接工厂

jms/阿耳特弥斯
Jms服务代码为:

package com.mycompany.jms;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TemporaryQueue;
import javax.jms.TextMessage;
import javax.naming.*;

import org.apache.aries.blueprint.annotation.service.Reference;
import org.apache.aries.blueprint.annotation.service.Service;
import org.apache.aries.blueprint.annotation.service.ServiceProperty;

import com.mycompany.JmsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Service(classes = JmsService.class, properties = {
// Only necessary for Remote Services
@ServiceProperty(name = "service.exported.interfaces", values = "*") })
@Singleton
public class JmsService4Reals implements JmsService {
private static final Logger LOG = LoggerFactory.getLogger(JmsService4Reals.class);

@Reference
ConnectionFactory connectionFactory;

@Override
public String sendMessage(String opeCod, String message) {
LOG.info(String.format("received: opeCod=%s, msg=%s", opeCod, message));
try {
    if(connectionFactory== null)
        return "Reference: no connectionFactory found";
    Connection connection = null;
    try {
        connection = connectionFactory.createConnection();
        LOG.info("JMS Connection created=%s", connection);
        connection.start();

    } catch (JMSException e) {
        if (connection != null)
            connection.stop();
        return prepareErrorResponse(e.getMessage());
    }
} catch (JMSException e) {
    return prepareErrorResponse(e.getMessage());
}
}

private String prepareErrorResponse(String msg) {
return msg;
}

}

请帮帮我,我陷入了毫无进展的困境
x一个一个一个一个x一个一个二个x
我希望jms/artem your text将被注入到我的ConnectionFactory中,但从未发生。

6vl6ewon

6vl6ewon1#

调用context.lookup()时得到的实际异常是:

javax.naming.NoInitialContextException: \
    Need to specify class name in environment or system property, \
    or as an applet parameter, or in an application resource file:  \
    java.naming.factory.initial

这就是JNDI的工作原理,在OSGi中使用它需要特别的准备(Fuse Karaf是基于Apache Karaf的OSGi运行时)。
您必须先安装jndi功能。然后您的例外将是:

javax.naming.NotContextException: jms/artemis

然而,它几乎是你所需要的一切。jndi功能为您提供了几个命令,像这样一个:

karaf@root()> jndi:names
JNDI Name                │ Class Name
─────────────────────────┼─────────────────────────────────────────────────────────────────
osgi:service/jms/artemis │ org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory
osgi:service/jndi        │ org.apache.karaf.jndi.internal.JndiServiceImpl

如果你现在使用osgi:service/jms/artemis而不是jms/artemis,你会得到正确的连接工厂。

2023-01-02 09:45:53,412 INFO  {XNIO-2 task-1} [grgr.test.Activator7$1.doGet()] \
(Activator7.java:65) : connectionFactory=ActiveMQConnectionFactory [serverLocator=ServerLocatorImpl \
[initialConnectors=[TransportConfiguration(name=null, factory=org-apache-activemq-artemis-core-remoting-impl-netty-NettyConnectorFactory) \
?port=61616&host=localhost], discoveryGroupConfiguration=null], \
clientID=null, consumerWindowSize = 1048576, \
dupsOKBatchSize=1048576, transactionBatchSize=1048576, readOnly=falseEnableSharedClientID=false]

你可以在Fuse Karaf中找到更多持久性用法的例子:https://github.com/jboss-fuse/karaf-quickstarts
开发人员文档如下:https://github.com/jboss-fuse/karaf-quickstarts/tree/7.x.redhat-7-11-x/persistence/manual/src/main/asciidoc

相关问题