3
respostas

Cannot instantiate class: org.apache.activemq.jndi.ActiveMQInitialContextFactory [Root exception is java.lang.ClassNotFoundException

Simplesmente não estou conseguindo rodar o consumer =/ fica dando a excecao abaixo, alguem tem uma ideia?

Codigo do consumer

public class TesteConsumidor {

    public static void main(String[] args) throws Exception {

        InitialContext context = new InitialContext();
        ConnectionFactory factory = (ConnectionFactory) context.lookup("ConnectionFactory");

        Connection connection = factory.createConnection();
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        Destination fila = (Destination) context.lookup("financeiro");
        MessageConsumer consumer = session.createConsumer(fila);

        consumer.setMessageListener(new MessageListener() {

            @Override
            public void onMessage(Message message) {

                TextMessage textMessage = (TextMessage) message;

                try {
                    System.out.println(textMessage.getText());
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }

        });

        session.close();
        connection.close();
        context.close();
    }
}

jndi.properties

java.naming.factory.initial = org.apache.activemq.jndi.ActiveMQInitialContextFactory
java.naming.provider.url = tcp://localhost:61616
queue.financeiro = fila.financeiro
topic.MyTopic = example.MyTopic
Excecao ao tentar rodar o consumer
Exception in thread "main" javax.naming.NoInitialContextException: Cannot instantiate class: org.apache.activemq.jndi.ActiveMQInitialContextFactory [Root exception is java.lang.ClassNotFoundException: org.apache.activemq.jndi.ActiveMQInitialContextFactory]
    at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:674)
    at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313)
    at javax.naming.InitialContext.init(InitialContext.java:244)
    at javax.naming.InitialContext.<init>(InitialContext.java:192)
    at com.black.jms.TesteProdutor.main(TesteProdutor.java:15)
Caused by: java.lang.ClassNotFoundException: org.apache.activemq.jndi.ActiveMQInitialContextFactory
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:348)
    at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:72)
    at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:61)
    at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:672)
    ... 4 more
3 respostas

Oi Thiago, tudo bem?

Está usando Maven? O jar do ActiveMQ está no seu classpath?

Abs

tenho duas dependencias.

<dependencies>
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-all</artifactId>
            <version>5.15.3</version>
        </dependency>
        <dependency>
            <groupId>javax.jms</groupId>
            <artifactId>jms</artifactId>
            <version>1.1</version>
        </dependency>
    </dependencies>

Movi o jndi.properties para src/main/resources no maven e adicionei mais uma dependencia.

        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-core</artifactId>
            <version>5.7.0</version>
        </dependency>

Agora funciona, não sei pq, mas funciona =D