简体   繁体   中英

Redhat Fuse-Karaf refusing to inject jms ConnectionFactory

Fuse karaf fuse-karaf-7.11.1.fuse-7_11_1-00013-redhat-00003.

I am creating a simple bridge from servlet to ActiveMQ 5.9 using amqp protocol. I managed to configure a ConectionFactory and tested OK with the jms:send command. I wrote a JMS service which is responding to the POST from the servlet side, but is failing to create the Connection factory.

admin@root()> jms:connectionFactories JMS Connection Factory

jms/artemis

The Jms service code is:

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;
}

}

Please help, i'm stuck with no progress

The servlet always reponds with "Reference: no connectionFactory found"

I also tried with the JNDI lookup method with the same result.

try {
    Context context = new InitialContext();
    LOG.info("context=%s", context);
    connectionFactory = (ConnectionFactory) context.lookup("jms/artemis");
    LOG.info("connectionFactory=%s", connectionFactory);
    }catch(Exception e) {
        e.printStackTrace();
        return "no connectionFactory found on JNDI";
    }

I expect the jms/artem your text is to be injected on my ConnectionFactory, but never occurs.

The actual exception you get when calling context.lookup() is:

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

That's how JNDI works and you need special preparation to use it in OSGi (and Fuse Karaf is OSGi runtime based on Apache Karaf).

You have to install jndi feature first. Then your exception will be:

javax.naming.NotContextException: jms/artemis

However it's almost everything you need. jndi feature gives you several commands, like this one:

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

If you now use osgi:service/jms/artemis instead of just jms/artemis , you get proper connection factory. I got this in logs:

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]

You can find more examples of persistence usage in Fuse Karaf here: https://github.com/jboss-fuse/karaf-quickstarts

A developer documentation is here: https://github.com/jboss-fuse/karaf-quickstarts/tree/7.x.redhat-7-11-x/persistence/manual/src/main/asciidoc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM