Hi Romain,

I tried to follow the steps mentioned in the thread but still I am getting
exceptions: I am using tomee 1.5.2
Any help would be greatly appreciated.
-Shan

java
-DResource/javax.jms.ConnectionFactory=MyJmsConnectionFactory:org.apache.activemq.ActiveMQConnectionFactory:tcp://localhost:61616
-cp
C:\AppServers\tomee\lib\activemq-core-5.7.0.jar;C:\AppServers\tomee\lib\slf4j-api-1.7.2.jar;C:\AppServers\tomee\lib\slf4j-jdk14-1.7.2.jar;C:\experiments\TomEEAppClient\dist\TomEEAppClient.jar
tomeeappclient.TomEEAppClient  
Resource/javax.jms.ConnectionFactory=MyJmsConnectionFactory:org.apache.activemq.ActiveMQConnectionFactory:tcp://localhost:61616
---------------------------------------------------------------------------------
Getting the OpenEJB InitialContext
----------------------------------
Apr 13, 2013 8:28:14 PM org.apache.openejb.client.EventLogger log
INFO:
RemoteInitialContextCreated{providerUri=http://localhost:8080/tomee/ejb}
{java.naming.factory.initial=org.apache.openejb.client.RemoteInitialContextFactory,
java.naming.provider.url=http://localhost:8080/tomee/ejb}

----------------------------------
Looking up MyJmsConnectionFactory 
----------------------------------
*java.lang.UnsupportedOperationException: Unsupported Naming URI scheme
'MyJmsConnectionFactory'*
        at 
org.apache.openejb.client.JNDIContext.parseEntry(JNDIContext.java:327)
        at org.apache.openejb.client.JNDIContext.lookup(JNDIContext.java:270)
        at javax.naming.InitialContext.lookup(Unknown Source)
        at
tomeeappclient.TomEEAppClient.lookupConnectionFactory(TomEEAppClient.java:88)
        at tomeeappclient.TomEEAppClient.testOpenEjbJNDI(TomEEAppClient.java:59)
        at tomeeappclient.TomEEAppClient.main(TomEEAppClient.java:27)
ConnectionFactory: null

----------------------------------
Listing all Context Entries
----------------------------------
-> . : java.lang.String
-> openejb : javax.naming.Context
-> -> UserBusinessRemote :
org.apache.openejb.core.ivm.naming.BusinessRemoteReference
-> -> DeployerBusinessRemote :
org.apache.openejb.core.ivm.naming.BusinessRemoteReference
-> -> ConfigurationInfoBusinessRemote :
org.apache.openejb.core.ivm.naming.BusinessRemoteReference
-> CachedInfoMessageHandlerBean :
org.apache.openejb.core.ivm.naming.IntraVmJndiReference
-> MEJB : org.apache.openejb.core.ivm.naming.ObjectReference
-> CalculatorBeanRemote :
org.apache.openejb.core.ivm.naming.BusinessRemoteReference
---------------------------------------------------------------------------------

---------------------------------------------------------------------------------
Getting the ActiveMQ InitialContext
----------------------------------
{java.naming.factory.initial=org.apache.activemq.jndi.ActiveMQInitialContextFactory,
java.naming.provider.url=http://localhost:61616}

----------------------------------
Looking up MyJmsConnectionFactory 
----------------------------------
*javax.naming.NameNotFoundException: MyJmsConnectionFactory*
        at
org.apache.activemq.jndi.ReadOnlyContext.lookup(ReadOnlyContext.java:235)
        at javax.naming.InitialContext.lookup(Unknown Source)
        at
tomeeappclient.TomEEAppClient.lookupConnectionFactory(TomEEAppClient.java:88)
        at 
tomeeappclient.TomEEAppClient.testActiveMqJNDI(TomEEAppClient.java:41)
        at tomeeappclient.TomEEAppClient.main(TomEEAppClient.java:29)
ConnectionFactory: null

----------------------------------
Listing all Context Entries
----------------------------------
-> dynamicTopics : org.apache.activemq.jndi.ActiveMQInitialContextFactory$2
-> TopicConnectionFactory : org.apache.activemq.ActiveMQConnectionFactory
-> ConnectionFactory : org.apache.activemq.ActiveMQConnectionFactory
-> XAConnectionFactory : org.apache.activemq.ActiveMQXAConnectionFactory
-> dynamicQueues : org.apache.activemq.jndi.ActiveMQInitialContextFactory$1
-> QueueConnectionFactory : org.apache.activemq.ActiveMQConnectionFactory
---------------------------------------------------------------------------------

*Resource defined in tomee.xml*
<Resource id="MyJmsResourceAdapter" type="ActiveMQResourceAdapter">
                BrokerXmlConfig = broker:(tcp://localhost:61616)
                ServerUrl = tcp://localhost:61616
        </Resource>

        <Resource id="MyJmsConnectionFactory" 
type="javax.jms.ConnectionFactory">
                ResourceAdapter = MyJmsResourceAdapter
        </Resource>

        <Container id="MyJmsMdbContainer" ctype="MESSAGE">
                ResourceAdapter = MyJmsResourceAdapter
        </Container>

        <Resource id="AnswerQueue" type="javax.jms.Queue" />

*Message Bean:*

import javax.annotation.Resource;
import javax.ejb.MessageDriven;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;


@MessageDriven
public class CachedInfoMessageHandlerBean implements MessageListener
{
    @Resource(name = "MyJmsConnectionFactory", type =
ConnectionFactory.class)
    // @Resource
    private ConnectionFactory connectionFactory;

    @Resource(name = "AnswerQueue")
    private Queue answerQueue;

    @Override
    public void onMessage(Message message) {
        try {

            final TextMessage textMessage = (TextMessage) message;
            final String question = textMessage.getText();

            if ("Hello World!".equals(question)) 
            {
                respond("Hello, Test Case!");
            } else if ("How are you?".equals(question)) 
            {
                respond("I'm doing well.");
            } else if ("Still spinning?".equals(question)) 
            {
                respond("Once every day, as usual.");
            }
        } catch (JMSException e) {
            throw new IllegalStateException(e);
        }
    }

    private void respond(String text) throws JMSException 
    {
        Connection connection = null;
        Session session = null;

        try {
            connection = connectionFactory.createConnection();
            connection.start();

            // Create a Session
            session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);

            // Create a MessageProducer from the Session to the Topic or
Queue
            MessageProducer producer = session.createProducer(answerQueue);
            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

            // Create a message
            TextMessage message = session.createTextMessage(text);

            // Tell the producer to send the message
            producer.send(message);
        } finally 
        {
            // Clean up
            if (session != null) 
            {
                session.close();
            }
            if (connection != null) 
            {
                connection.close();
            }
        }
    }
}

*Client Code :*

import java.util.Hashtable;
import javax.jms.ConnectionFactory;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NameClassPair;
import javax.naming.NameNotFoundException;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.NotContextException;

/**
 *
 * 
 */
public class TomEEAppClient 
{
    /**
         * @param args
         * @throws NamingException
         */
        public static void main(String[] args) {
                
                System.out.println("Resource/javax.jms.ConnectionFactory=" +
System.getProperty("Resource/javax.jms.ConnectionFactory"));

                testOpenEjbJNDI();
                
                testActiveMqJNDI();
        }

        private static void testActiveMqJNDI() {
                InitialContext context;
                ConnectionFactory cf;
        
System.out.println("---------------------------------------------------------------------------------");
                System.out.println("Getting the ActiveMQ InitialContext");
                System.out.println("----------------------------------");
                
                context = getActiveMQInitialContext();
                
                lookupConnectionFactory(context);
                
                listContextEntries(context, "");
                
        
System.out.println("---------------------------------------------------------------------------------");
                System.out.println();
        }

        private static void testOpenEjbJNDI() {
                InitialContext context;
                ConnectionFactory cf;
                
        
System.out.println("---------------------------------------------------------------------------------");
                System.out.println("Getting the OpenEJB InitialContext");
                System.out.println("----------------------------------");
                
                context = getOpenEJBInitialContext();
                
                lookupConnectionFactory(context);
                
                listContextEntries(context, "");
                
        
System.out.println("---------------------------------------------------------------------------------");
                System.out.println();
        }

        private static InitialContext getInitialContext(Hashtable<String, 
String>
ctxProps) {
                InitialContext ctx = null;
                try {
                        ctx = new InitialContext(ctxProps);
                        System.out.println(ctx.getEnvironment());
                } catch (NamingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                return ctx;
        }

        private static void lookupConnectionFactory(InitialContext context) {
                
                ConnectionFactory cf =  null;
                try {
                        System.out.println();
                        
System.out.println("----------------------------------");
                        System.out.println("Looking up MyJmsConnectionFactory 
");
                        
System.out.println("----------------------------------");
                        String factory = "MyJmsConnectionFactory";
                        cf = (ConnectionFactory)context.lookup(factory);
                } catch (NamingException e)
                {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }catch( Exception ex)
                {
                    ex.printStackTrace();
                }
                System.out.println("ConnectionFactory: " + cf);
        }

        private static InitialContext getOpenEJBInitialContext() {

                Hashtable<String, String> ctxProps = new Hashtable<String, 
String>(4);
                ctxProps.put("java.naming.factory.initial",
"org.apache.openejb.client.RemoteInitialContextFactory");
                ctxProps.put("java.naming.provider.url",
"http://localhost:8080/tomee/ejb";);
                // ctxProps.put("java.naming.security.principal", "tomee");
                // ctxProps.put("java.naming.security.credentials", "tomee");

                return getInitialContext(ctxProps);
        }

        private static InitialContext getActiveMQInitialContext() {

                Hashtable<String, String> ctxProps = new Hashtable<String, 
String>(2);
                ctxProps.put("java.naming.factory.initial",
"org.apache.activemq.jndi.ActiveMQInitialContextFactory");
                ctxProps.put("java.naming.provider.url", 
"http://localhost:61616";);

                return getInitialContext(ctxProps);
        }
        
        private static int contextCount = 0;

        private static void listContextEntries(Context ctx, String 
namedContext) {
                if (0 == contextCount) {
                        System.out.println();
                        
System.out.println("----------------------------------");
                        System.out.println("Listing all Context Entries");
                        
System.out.println("----------------------------------");
                }
                NamingEnumeration<NameClassPair> list;
                try {
                        list = ctx.list(namedContext);
                        
                        if (null != list) {
                                while (list.hasMore()) {
                                        final NameClassPair pair = list.next();
                                        final String name = pair.getName();
                                        final String className = 
pair.getClassName();
                                        
                                        for(int count = 0; count <= 
contextCount; count++) {
                                                System.out.print("-> ");
                                        }

                                        System.out.println(name + " : " + 
className);
                                        contextCount++;
                                        if(contextCount > 1) {
                                                listContextEntries(ctx, 
namedContext + '/' + name);
                                        } else {
                                                listContextEntries(ctx, name);
                                        }
                                        contextCount--;
                                }
                        }
                } catch (NameNotFoundException e) {
                        // not a valid context...
                        e.printStackTrace(System.out);
                } catch (NotContextException e) {
                        // not a valid context...
                } catch (NamingException e) {
                        e.printStackTrace(System.out);
                }

        }
}  
    






--
View this message in context: 
http://openejb.979440.n4.nabble.com/Can-not-connect-to-Message-Bean-from-remote-client-Null-pointer-tp4662203p4662220.html
Sent from the OpenEJB User mailing list archive at Nabble.com.

Reply via email to