4

I never work on JMS. Recently I downloaded Activemq and changed port no from 61616 to 61617 in all conf/activemq-*.xml files.I run the following command from command prompt and open console page on browser.

  C:\Users\Infratab Bangalore\Desktop\Queueing\apache-activemq-5.8.0\bin>activemq

enter image description here

Now I want to send messages from java code using JMS to Activemq.For this I wrote the following code. And run my code using Apache Tomcat server.it's not working

This code is implemented in Eclipse.

package PackageName;

import java.io.IOException;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.activemq.ActiveMQConnectionFactory;

public class MessageProducer extends HttpServlet {
    @Override
    protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
        try {
            //created ConnectionFactory object for creating connection 
            ConnectionFactory factory = new ActiveMQConnectionFactory("admin", "admin", "tcp://localhost:61617");
            //Establish the connection
            Connection connection = factory.createConnection();
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            Queue queue = session.createQueue("Test");
            //Added as a producer
            javax.jms.MessageProducer producer = session.createProducer(queue);
            // Create and send the message
            TextMessage msg = session.createTextMessage();
            msg.setText("TestMessage");
            producer.send(msg);
        } catch (Exception e) {
            // TODO: handle exception
        }
    }
}

I am getting the following error

 java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
org.apache.activemq.ActiveMQPrefetchPolicy.<clinit>(ActiveMQPrefetchPolicy.java:30)
org.apache.activemq.ActiveMQConnectionFactory.<init>(ActiveMQConnectionFactory.java:88)
PackageName.MessageProducer.service(MessageProducer.java:20)
javax.servlet.http.HttpServlet.service(HttpServlet.java:723)

can you suggest me, where I wrote wrong.

Thanks.

Ashish Aggarwal
  • 3,018
  • 2
  • 23
  • 46
Hanumath
  • 1,117
  • 9
  • 23
  • 41
  • The ActiveMQ is probably using commons-logging.jar but seems like it's not being found in your classpath. Check that out – Diego Urenia Aug 08 '13 at 11:35
  • @drurenia I added three .jar files in my lib folder names are `activemq-all-5.0.0.jar`, `commons-io-2.4.jar`, `geronimo-spec-corba-1.0.jar`.I added the following path in classPath. `C:\Users\Infratab Bangalore\Desktop\Queueing\geronimo-spec-corba-1.0.jar;`.So am I need to add `commons-logging.jar` in my classpath.If I need to add again I want to download it.is it right – Hanumath Aug 08 '13 at 11:39
  • Yes, seems like you're not using maven, then you have to download the *commons-logging.jar* and add to your classhpath, exactly as you did with the *activemq-all-5.0.0.jar* – Diego Urenia Aug 08 '13 at 11:46
  • I added `activemq-all-5.0.0.jar` in my lib folder not in classpath.can you see the comment what I put on @Ashish answer. – Hanumath Aug 08 '13 at 11:52
  • 1
    i think you should read that to understand what i'm saying: http://stackoverflow.com/questions/325524/where-to-put-the-external-jars – Diego Urenia Aug 08 '13 at 11:54
  • @drurenia Thank you for your valuable link,It's working great.can you tell me Right now I added 3 .jar files,which are needed for this task.Because I want to delete unnecessery jar files.Again Really Thank you. – Hanumath Aug 08 '13 at 12:02

2 Answers2

5

Download common-logging.ja r file from

http://grepcode.com/snapshot/repo1.maven.org/maven2/commons-logging/commons-logging/1.1.3/

and place this into your classpath and run again.

Ashish Aggarwal
  • 3,018
  • 2
  • 23
  • 46
  • I downloaded `commons-logging-1.1.3.zip` file and placed under `C:\Users\Infratab Bangalore\Desktop\Queueing` folder. I added classpath like `C:\Users\Infratab Bangalore\Desktop\Queueing\commons-logging-1.1.3`.I restarted my eclipse,still it's not working. – Hanumath Aug 08 '13 at 11:50
  • I added `commons-logging-1.1.3.jar` file in my lib folder.It's working great. – Hanumath Aug 08 '13 at 12:07
2

The message is from the line 38 of ActiveMQPrefetchPolicy which occurs during the initialization of the class (due it is an static field) (example of the line in grepcode http://grepcode.com/file/repository.springsource.com/org.apache.activemq/com.springsource.org.apache.activemq/5.3.0/org/apache/activemq/ActiveMQPrefetchPolicy.java#38 ) . You will need common-logging.jar as it is a dependency in the classpath of your application to run. You might get other errors. I would recommend you to follow some sample in the internet, for example the example of the Chapter 8 of the ActiveMQ in Action -> http://code.google.com/p/activemq-in-action/source/browse/#svn%2Ftrunk%2Fexamples%2Fchapter8%2Fjms-webapp-jboss%253Fstate%253Dclosed

Regards,
Luan

lcestari
  • 178
  • 5
  • I am getting following warning,while sending messages to `Activemq`. `log4j:WARN No appenders could be found for logger (org.apache.activemq.transport.WireFormatNegotiator). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.` How can I fix – Hanumath Aug 08 '13 at 14:58