How to use PooledConnectionFactoryBean with ActiveMQ - java

First of all, I am new to JSM.
I am trying to create a pool of connection for JMS ActiveMQ
Using the connection factory ActiveMQConnectionFactory I succeed to send/receive messages, but using the PooledConnectionFactoryBean it failed with this error:
Exception in thread "Thread-0" java.lang.NoClassDefFoundError: org/springframework/beans/factory/FactoryBean
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at jmxproducer.JMXProducerTest.run(JMXProducerTest.java:28)
at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.ClassNotFoundException: org.springframework.beans.factory.FactoryBean
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
... 14 more
Exception in thread "Thread-1" java.lang.NoClassDefFoundError: org/apache/activemq/pool/PooledConnectionFactoryBean
at jmxproducer.JMXProducerTest.run(JMXProducerTest.java:28)
at java.lang.Thread.run(Thread.java:722)
Exception in thread "Thread-2" java.lang.NoClassDefFoundError: org/apache/activemq/pool/PooledConnectionFactoryBean
at jmxproducer.JMXProducerTest.run(JMXProducerTest.java:28)
at java.lang.Thread.run(Thread.java:722)
Exception in thread "Thread-3" java.lang.NoClassDefFoundError: org/apache/activemq/pool/PooledConnectionFactoryBean
at jmxproducer.JMXProducerTest.run(JMXProducerTest.java:28)
at java
.lang.Thread.run(Thread.java:722)
Here is the source code:
package jmxproducer;
import javax.jms.*;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.pool.PooledConnectionFactoryBean;
public class JMXProducerTest implements Runnable {
private String message;
public JMXProducerTest(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
#Override
public void run() {
try {
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
PooledConnectionFactoryBean pool = new PooledConnectionFactoryBean();
pool.setConnectionFactory(connectionFactory);
// Create a Connection
Connection connection = connectionFactory.createConnection();
connection.start();
// Create a Session
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Create the destination (Topic or Queue)
Destination destination = session.createQueue("MY.QUEUE");
// Create a MessageProducer from the Session to the Topic or Queue
MessageProducer producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
// Create a messages
TextMessage msg = session.createTextMessage(message);
producer.send(msg);
session.close();
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
What did I miss? I added to the classpath this jar: activemq-all-5.8.0.jar
Thanks,
kdureidy

Did you add the Spring libraries to your project?
The error
java.lang.ClassNotFoundException: org.springframework.beans.factory.FactoryBean
seems to indicate that spring-beans-X.jar is missing from your project (where X is the version of Spring that you're missing).

Related

Cannot instantiate class: org.apache.qpid.jndi.PropertiesFileInitialContextFactory

I am writing a Qpid client that sends a message. The example code that I am using is :
private void runTest() throws Exception {
Properties properties = new Properties();
properties.load(this.getClass().getResourceAsStream("helloworld.properties"));
Context context = new InitialContext(properties);
ConnectionFactory connectionFactory = (ConnectionFactory) context.lookup("qpidConnectionFactory");
Connection connection = connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
Queue queue = (Queue) context.lookup("myqueue");
MessageConsumer messageConsumer = session.createConsumer(queue);
MessageProducer messageProducer = session.createProducer(queue);
TextMessage message = session.createTextMessage("Hello world!");
messageProducer.send(message);
session.commit();
message = (TextMessage)messageConsumer.receive();
session.commit();
System.out.println(message.getText());
connection.close();
context.close();
}
The accompanying config is :
java.naming.factory.initial = org.apache.qpid.jndi.PropertiesFileInitialContextFactory
connectionfactory.qpidConnectionFactory = amqp://guest:guest#clientid/?brokerlist='tcp://localhost:61111'
queue.myqueue = queue1
The ERROR that I am getting is :
Exception in thread "main" javax.naming.NoInitialContextException: Cannot instantiate class: org.apache.qpid.jndi.PropertiesFileInitialContextFactory [Root exception is java.lang.ClassNotFoundException: org.apache.qpid.jndi.PropertiesFileInitialContextFactory]
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:216)
at Hello.runTest(Hello.java:19)
at Hello.main(Hello.java:13)
Caused by: java.lang.ClassNotFoundException: org.apache.qpid.jndi.PropertiesFileInitialContextFactory
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
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)
... 5 more
Looks like it is unable to initialize the context for the Properties file.
I have an ActiveMQ instance running locally on port 61111. The Properties file is indeed in my CLASSPATH. What else could I be missing here ?
Try java.naming.factory.initial = org.apache.qpid.jms.jndi.JmsInitialContextFactory

RabbitMQ connection exception

I tried using the rabbitMQ and while creating the connection I got the below given exception with output, I am not sure what went wrong.
The factory is working fine and the hostname,port , username and password are setting correctly, but the connection is failing each time.
In main
parametres setted , now creating the connection
The host name and other params are localhost
The host name and other params are admin
The host name and other params are admin
The host name and other params are 5672
setted all the params
*Exception in thread "main" java.io.IOException
at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:106)
at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:102)
at com.rabbitmq.client.impl.AMQConnection.start(AMQConnection.java:347)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:516)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:533)
at com.TestSend.main(TestSend.java:39)
Caused by: com.rabbitmq.client.ShutdownSignalException: connection error; reason: java.lang.NoClassDefFoundError: org/apache/commons/io/input/ProxyInputStream
at com.rabbitmq.utility.ValueOrException.getValue(ValueOrException.java:67)
at com.rabbitmq.utility.BlockingValueOrException.uninterruptibleGetValue(BlockingValueOrException.java:33)
at com.rabbitmq.client.impl.AMQChannel$BlockingRpcContinuation.getReply(AMQChannel.java:343)
at com.rabbitmq.client.impl.AMQConnection.start(AMQConnection.java:300)
... 3 more
Caused by: java.lang.NoClassDefFoundError: org/apache/commons/io/input/ProxyInputStream
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at com.rabbitmq.client.impl.AMQImpl.readMethodFrom(AMQImpl.java:3205)
at com.rabbitmq.client.impl.CommandAssembler.consumeMethodFrame(CommandAssembler.java:93)
at com.rabbitmq.client.impl.CommandAssembler.handleFrame(CommandAssembler.java:158)
at com.rabbitmq.client.impl.AMQCommand.handleFrame(AMQCommand.java:87)
at com.rabbitmq.client.impl.AMQChannel.handleFrame(AMQChannel.java:89)
at com.rabbitmq.client.impl.AMQConnection$MainLoop.run(AMQConnection.java:500)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.io.input.ProxyInputStream
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 18 more*
The code I wrote was
package com;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import com.rabbitmq.client.ConnectionFactory;
public class TestSend {
public static void main(String[] args) throws IOException {
System.out.println("In main ");
// Setting up connection
ConnectionFactory factory;
factory = new ConnectionFactory();
String host = "localhost";
String password = "admin";
String user = "admin";
String port = "5672";
System.out.println(" parametres setted , now creating the connection");
factory.setHost(host);
factory.setPassword(password);
factory.setUsername(user);
factory.setPort(Integer.parseInt(port));
factory.setRequestedHeartbeat(30);
System.out.println("The host name and other params are " + factory.getHost());
System.out.println("The host name and other params are " + factory.getUsername());
System.out.println("The host name and other params are " + factory.getPassword());
System.out.println("The host name and other params are " + factory.getPort());
System.out.println("Setted all the params");
Connection connection;
connection = factory.newConnection();
System.out.println("Establishing the connection " + host);
String s = "Hello";
Channel channel; // This channel sends the file
channel = connection.createChannel();
System.out.println("declaring Queue now");
channel.queueDeclare("nav", true, false, false, null);
channel.basicPublish("", "nav", null, s.getBytes());
System.out.println(" seems like everything has been sent to the queue ");
System.out.println("Closing the connection");
channel.close();
connection.close();
}
}
I just found the solution to this .
I added the common-io-2.5.jar file in the project and after re-building it ,it worked.
The jar was obtained from the link : http://commons.apache.org/proper/commons-io/download_io.cgi

Apache Spark MySQL JavaRDD.foreachPartition - why I getting ClassNotFoundException

I would like to save data from each partition to MySQL Database. For doing that I created Class which implements VoidFunction<> :
public class DatabaseSaveFunction implements VoidFunction<Iterator<String>> {
/**
*
*/
private static final long serialVersionUID = -7039277486852158360L;
public void call(Iterator<String> it) {
Connection connect = null;
PreparedStatement preparedStatement = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connect = DriverManager.getConnection("jdbc:mysql://"
+ "xxx.us-west-2.rds.amazonaws.com" + "/"
+ "xxx", "xxx", "xxx");
preparedStatement = connect
.prepareStatement("insert into testdatabase.test values (default, ?)");
while (it.hasNext()) {
String outputElement = it.next();
preparedStatement.setString(1, "" + outputElement.length());
preparedStatement.executeUpdate();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
connect.close();
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
And in my main method class I'm calling:
output.foreachPartition(new DatabaseSaveFunction());
I'm getting following error:
15/05/06 15:34:00 WARN scheduler.TaskSetManager: Lost task 0.0 in stage 1.0 (TID 4, ip-172-31-36-44.us-west-2.compute.internal): java.lang.ClassNotFoundException: DatabaseSaveFunction
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:274)
Worker log:
15/05/06 15:34:00 ERROR executor.Executor: Exception in task 1.0 in stage 1.0 (TID 5)
java.lang.ClassNotFoundException: DatabaseSaveFunction
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:274)
Can anybody tell me what I'm doing wrong ? I would be very grateful for that.
Export the external class to jar and add that like sc.addJar("/path/x.jar") where sc is JavaSparkContext in your main. Then you wont get this error. The error is because you spark program is not able to find the class. Moreover in spark 1.3 and greater you can simple use a map options of jdbc and then use load("jdbc", options) to create a data frame and load data from any RDBMS. its really handy. I am not sure if this method works for connecting any RDBMS into spark. Please tell me if you have any other question.

How to lookup EJB remoting from a EJB application into a java application?

I'm working on a tow projects , EJB project (where i have all my EJB Remote) and the other project is a simple java application with main method. but when i try to lookup an EJB statless and remote via JNDI , it doesn't work.
Really i used many way but no good results.
this is the code and the consol error .
1) My Ejb project when i clean and install it , it gives me a jar file.
2) I use Jboss EAP6.
3) i have a jndi.properties insid of src/main/ressouces where i have this :
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url = localhost:1099
PersonalFactory.java (EJB of the EJB project)
import javax.ejb.Remote;
import javax.ejb.Stateless;
import com.lazrak.common.logging.CommonLogger;
import com.lazrak.remotes.IPersonFactoryRemote;
#Remote(IPersonFactoryRemote.class)
#Stateless
public class PersonFactory implements IPersonFactoryRemote
#Override
public void findMe() {
System.out.println("I'am inside of the method findMe");
}
MyMainClass.java (where i have the main method and it is in the second java project)
All import ...
public class MyMainClass {
public static void main(String[] args) throws Exception {
System.out.println("Main start ");
invokeStatelessBean();
}
private static void invokeStatelessBean(){
Hashtable props = new Hashtable();
props.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
props.put(Context.URL_PKG_PREFIXES,"org.jboss.ejb.client.naming");
props.put(Context.PROVIDER_URL,"jnp://localhost:1099");
InitialContext context = null;
try {
context = new InitialContext(props);
} catch (NamingException e) {
System.out.println("Faild to add properties to InitialContext"+e.getMessage());
}
try {
final IPersonFactoryRemote statelessRemoteCalculator = (IPersonFactoryRemote) context
.lookup("ejb:"+moduleName+"/"+PersonFactory.class.getSimpleName();+"/"+IPersonFactoryRemote.class.getName());
} catch (NamingException e) {
System.out.println("EJB Remot doesn't been fined\n"+e.getMessage());
}
}
}
when i run my main application , it gives me that :
Main start
enter 2014-09-26 15:57:26,098 DEBUG org.jboss.logging - InitialContextFactory.java:64 - Logging Provider: org.jboss.logging.Log4jLoggerProvider
Exception in thread "main" java.lang.NoClassDefFoundError: org/jboss/remoting3/spi/ConnectionProviderFactory
at org.jboss.naming.remote.client.InitialContextFactory.<clinit>(InitialContextFactory.java:99)
at org.jboss.naming.remote.client.InitialContextFactory.<clinit>(InitialContextFactory.java:99)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:270)
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:671)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:307)
at javax.naming.InitialContext.init(InitialContext.java:242)
at javax.naming.InitialContext.<init>(InitialContext.java:216)
at RemoteEJBClient.invokeStatelessBean(RemoteEJBClient.java:42)
at RemoteEJBClient.main(RemoteEJBClient.java:18)
Caused by: java.lang.ClassNotFoundException:
org.jboss.remoting3.spi.ConnectionProviderFactory
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 11 more
props.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
props.put(Context.URL_PKG_PREFIXES,"org.jboss.ejb.client.naming");
props.put(Context.PROVIDER_URL,"jnp://localhost:1099");
Those properties are used for connecting to an older jboss version.. For eap 6, you have to use remoting instead of jnp.
HereĀ“s a tutorial that will show you how to connect
http://www.mastertheboss.com/jboss-server/jboss-as-7/jboss-as-7-remote-ejb-client-tutorial

NoClassDefFoundError while trying to use NxParser

I am trying to read a N-Triples (.nt) DBpedia file with NxParser, but I had the following error, and I don't know what to do.
Exception in thread "main" java.lang.NoClassDefFoundError: org/semanticweb/yars/nx/parser/NxParser$1
at org.semanticweb.yars.nx.parser.NxParser.stringItFromBufferedReader(Unknown Source)
at org.semanticweb.yars.nx.parser.NxParser.<init>(Unknown Source)
at org.semanticweb.yars.nx.parser.NxParser.<init>(Unknown Source)
at SentencesMatching_prova.main(SentencesMatching_prova.java:29)
Caused by: java.lang.ClassNotFoundException: org.semanticweb.yars.nx.parser.NxParser$1
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
... 4 more
The source code of the script is:
import java.io.*;
import org.semanticweb.yars.nx.parser.*;
public class SentencesMatching_prova {
public static void main(String[] args) {
try {
String relationFileName = "../zzz-trash/revisions_en.nt";
FileInputStream is = new FileInputStream(relationFileName);
NxParser nxp = new NxParser(is);
while (nxp.hasNext()) {
// do stuff
}
} catch (Exception ex) {
ex.printStackTrace(System.out);
}
}
}

Categories

Resources