2

我是 OSGi 和 JNDI 的新手。我正在尝试使用 HornetQ 打包一个 JMS 客户端。为此,我使用 JNDI。据我所知,我需要使用 Apache Aries 来做到这一点。

感谢 iPOJO,我得到了 JNDIContextManager,这似乎运作良好。必须有一个 Apache Aries 捆绑包来注册它,这样我才能得到它。由于 JNDIContextManager.newInitialcontest,当我尝试获取新的 InitialContext 时,问题就出现了。

所以,这是我的代码:

import java.util.Properties;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.naming.Context;
import javax.naming.NamingException;

import org.hornetq.jms.referenceable.ConnectionFactoryObjectFactory;
import org.jnp.interfaces.NamingContextFactory;
import org.osgi.framework.BundleContext;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
import org.osgi.service.jndi.JNDIContextManager;
import org.usenet.logger.Logger;

public class JbossJmsClient implements JbossJmsClientService{

    private static final Logger logger = Logger.getLogger("JbossJmsClient");

    private String fullAddress = null;
    private boolean transacted = false;
    private Connection connection = null;
    private Session session = null;
    private MessageConsumer consumer = null;
    private JNDIContextManager contextManager;
    private MessageListener messageListener;
    private BundleContext bundleContext;

    public JbossJmsClient(JNDIContextManager contextManager,BundleContext bC){
        this.contextManager = contextManager;   
        this.bundleContext = bC;
    }

    public void start() {
        ConnectionFactory connectionFactory = null;
        Destination dest = null;
        Context jndiContext = null;
        /*
         * Create a JNDI API InitialContext object if none exists yet.
         */
        try {

            logger.debug(contextManager.toString());

            ClassLoader oCL = Thread.currentThread().getContextClassLoader();

            jndiContext = contextManager.newInitialContext(getJavaNamingProperties());

            logger.debug("jndiContext: "+jndiContext);
        } catch (NamingException e) {
            logger.error("Could not create JNDI API context: " + e.toString());
            e.printStackTrace();
            if(jndiContext == null){
                logger.error("5555555555555555555555555555555555555555555555555555555555 jndiContext == null!!!");
                return;
            }
        } 

        /*
         * Look up connection factory and destination. If either does not exist, exit. If you look up a TopicConnectionFactory or a QueueConnectionFactory, program behavior is the same.
         */
        try {
            logger.debug("Context: "+ jndiContext);

            connectionFactory = (ConnectionFactory) jndiContext.lookup("/ConnectionFactory");
            logger.debug("CF: "+ connectionFactory);
            dest = (Destination) jndiContext.lookup("/topic/test");
            logger.debug("Dest: "+ dest);
        } catch (Exception e) {
            logger.error("JNDI API lookup failed: " + e.toString());
            e.printStackTrace();
        }
        /*
         * Create connection. Create session from connection; false means session is not transacted. Create consumer, then start message delivery. Receive all text messages from destination until a
         * non-text message is received indicating end of message stream. Close connection.
         */
        try {
            connection = connectionFactory.createConnection();
            session = connection.createSession(transacted, Session.AUTO_ACKNOWLEDGE);
            consumer = session.createConsumer(dest);
            connection.start();
            consumer.setMessageListener(messageListener);
        } catch (JMSException e) {
            logger.error("Exception occurred: " + e.toString());
            e.printStackTrace();
        }
    }

    public void stop(){
        try {
            consumer.close();
            session.close();
            connection.close();
        } catch (Exception e){
            logger.error("Problem while closing");
        }
    }

    private Properties getJavaNamingProperties() throws NamingException {
        Properties returnProps = new Properties();
        returnProps.put(Context.INITIAL_CONTEXT_FACTORY,NamingContextFactory.class.getName());

        returnProps.put("osgi.service.jndi.bundleContext", bundleContext);
        returnProps.put(Context.PROVIDER_URL, fullAddress);
        returnProps.put(Context.OBJECT_FACTORIES, ConnectionFactoryObjectFactory.class.getName());
        returnProps.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
        return returnProps;
    }

    public String getFullAddress() {
        return fullAddress;
    }

    public void setFullAddress(String fullAddress) {
        this.fullAddress = fullAddress;
    }

    public MessageListener getMessageListener() {
        return messageListener;
    }

    public void setMessageListener(MessageListener messageListener) {
        this.messageListener = messageListener;
    }

    public boolean isTransacted() {
        return transacted;
    }

    public void setTransacted(boolean transacted) {
        this.transacted = transacted;
    }

}

我明白了:

[error][JbossJmsClient] Could not create JNDI API context: javax.naming.NoInitia
lContextException: Unable to find the InitialContextFactory org.jnp.interfaces.N
amingContextFactory.
javax.naming.NoInitialContextException: Unable to find the InitialContextFactory
 org.jnp.interfaces.NamingContextFactory.
        at org.apache.aries.jndi.ContextHelper.getInitialContext(ContextHelper.j
ava:148)
        at org.apache.aries.jndi.ContextManagerService.getInitialContext(Context
ManagerService.java:75)
        at org.apache.aries.jndi.ContextManagerService.newInitialContext(Context
ManagerService.java:61)
        at myClient.jbossjms.JbossJmsClient.start(JbossJmsClient
.java:75)
        at myClientUser.Activator$1.addingService(
Activator.java:44)
        at org.osgi.util.tracker.ServiceTracker$Tracked.customizerAdding(Service
Tracker.java:980)
        at org.osgi.util.tracker.ServiceTracker$Tracked.customizerAdding(Service
Tracker.java:906)
        at org.osgi.util.tracker.AbstractTracked.trackAdding(AbstractTracked.jav
a:262)
        at org.osgi.util.tracker.AbstractTracked.track(AbstractTracked.java:234)
at org.osgi.util.tracker.ServiceTracker$Tracked.serviceChanged(ServiceTr
acker.java:941)
        at org.apache.felix.framework.util.EventDispatcher.invokeServiceListener
Callback(EventDispatcher.java:932)
        at org.apache.felix.framework.util.EventDispatcher.fireEventImmediately(
EventDispatcher.java:793)
        at org.apache.felix.framework.util.EventDispatcher.fireServiceEvent(Even
tDispatcher.java:543)
        at org.apache.felix.framework.Felix.fireServiceEvent(Felix.java:4252)
        at org.apache.felix.framework.Felix.registerService(Felix.java:3275)
        at org.apache.felix.framework.BundleContextImpl.registerService(BundleCo
ntextImpl.java:346)
        at org.apache.felix.framework.BundleContextImpl.registerService(BundleCo
ntextImpl.java:320)
        at org.apache.felix.ipojo.IPojoContext.registerService(IPojoContext.java
:357)

我花了很多时间,我无法解决问题。任何帮助,将不胜感激。

提前致谢 ;)

4

0 回答 0