RabbitMQ connection exception - java

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

Related

javax.naming.ConfigurationException: Problem with PortableRemoteObject.toStub(); object not exported or stub not found

I am writing a Iiop server and I encountered the following error when I try to run it:
Exception in thread "main" javax.naming.ConfigurationException: Problem with PortableRemoteObject.toStub(); object not exported or stub not found [Root exception is java.rmi.NoSuchObjectException: object not exported]
at com.sun.jndi.toolkit.corba.CorbaUtils.remoteToCorba(CorbaUtils.java:105)
at com.sun.jndi.cosnaming.RemoteToCorba.getStateToBind(RemoteToCorba.java:79)
at javax.naming.spi.NamingManager.getStateToBind(NamingManager.java:870)
at com.sun.jndi.cosnaming.CNCtx.callBindOrRebind(CNCtx.java:600)
at com.sun.jndi.cosnaming.CNCtx.rebind(CNCtx.java:713)
at com.sun.jndi.cosnaming.CNCtx.rebind(CNCtx.java:730)
at javax.naming.InitialContext.rebind(InitialContext.java:433)
at ExecRmiIiopServ.<init>(ExecRmiIiopServ.java:13)
at ExecRmiIiopServ.main(ExecRmiIiopServ.java:20)
Caused by: java.rmi.NoSuchObjectException: object not exported
at sun.rmi.transport.ObjectTable.getStub(ObjectTable.java:124)
at java.rmi.server.RemoteObject.toStub(RemoteObject.java:106)
at com.sun.corba.se.impl.javax.rmi.PortableRemoteObject.toStub(PortableRemoteObject.java:156)
at javax.rmi.PortableRemoteObject.toStub(PortableRemoteObject.java:116)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.jndi.toolkit.corba.CorbaUtils.remoteToCorba(CorbaUtils.java:99)
... 8 more
From what I've read about this error, I need a strongly typed declarations of an object so the Garbage Collector won't discard it, hence why I tried to add a functions object that does just that, but still the error persists, both in the normal run and with a breakpoint, the error triggering at the ctx.rebind line.
public class ExecRmiIiopServ {
public ExecRmiIiopServ(String host, String port, String nume) throws Exception {
Runtime.getRuntime().exec("cmd /c start orbd -ORBInitialPort 1050");
Properties props = new Properties();
props.setProperty("java.naming.factory.initial", "com.sun.jndi.cosnaming.CNCtxFactory");
props.setProperty("java.naming.provider.url", "iiop://" + host + ":" + port);
ExecRmiInte functions = new ExecRmiImpl();
Context ctx = new InitialContext(props);
ctx.rebind(nume, functions);
System.out.println("Java RMI IIOP waiting: " + host + ":" + port + "/" + nume);
}
public static void main(String args[]) throws Exception {
if (args.length > 2)
new ExecRmiIiopServ(args[0], args[1], args[2]);
else
new ExecRmiIiopServ("localhost", "1050", "ExecIiop");
}
}
The ExecRmiImpl() class is just this for now:
public class ExecRmiImpl implements ExecRmiInte {
public ExecRmiImpl() { }
}
What am I doing wrong?

Not able to connect to AWS RDS MySql DB from AWS Lambda. ClassNotFoundException: com.mysql.jdbc

I am using maven project and added mysql connector dependency in pom.xml file and hence I am able to hit the RDS MySql db correctly.
But when I am deploying the maven build jar to AWS Lambda and trying to hit the function using API Gateway call, it's showing below error in cloudwatch.
*Error :*
com.mysql.jdbc.Driver: java.lang.ClassNotFoundException
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at com.project.Apigateway.App.handleRequest(App.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
I don't understand why the dependency is not resolved when I am deploying the project jar to AWS Lambda.
Please help me with your valuable suggestion.
public Connection makeJDBCConnection() throws ClassNotFoundException {
try {
Class.forName("com.mysql.jdbc.Driver");
String userName = "*****";
String password = "*****";
String dbName = "user";
String jdbcUrl = "jdbc:mysql://savetemp.cga2ptefm4rq.ap-south-1.rds.amazonaws.com:3306/" + dbName + "?user="
+ userName + "&password=" + password;
crunchifyConn = DriverManager.getConnection(jdbcUrl);
if (crunchifyConn != null) {
connected = true;
}
} catch (Exception ex) {
System.out.println("MySQL Connection Failed!" + ex);
}
return crunchifyConn;
}
Make sure that you have this package installed.

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

java.net.UnknownHostException when attempting to connect to database

I am attempting to connect to an Oracle database through Java with the Oracle JDBC driver with the following code (obscuring the host, service, user, and password):
import java.sql.*;
public class Main {
public Main () {
try {
String host = "HOST_NAME";
String port = "1521";
String service = "SERVICE_NAME";
String user = "SCHEMA_USER";
String password = "SCHEMA_PASSWORD";
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection connection = DriverManager.getConnection("jdbc:oracle:thin:#(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=" +
host +
")(PORT=" +
port +
")))(CONNECT_DATA=(SERVICE_NAME=" +
service +
")))",
user,
password);
connection.close ();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main (String args) {
new Main ();
}
}
However, I receive the following error:
java.sql.SQLException: IO Error: The Network Adapter could not establish the connection
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:458)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:546)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:236)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:521)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.acxiom.axle.reporting.database.DatabaseConnection.connect(DatabaseConnection.java:23)
at com.acxiom.axle.reporting.Reporting.establishDatabaseConnection(Reporting.java:53)
at com.acxiom.axle.reporting.Reporting.beginReporting(Reporting.java:20)
at com.acxiom.axle.reporting.Entry.<init>(Entry.java:28)
at com.acxiom.axle.reporting.Entry.main(Entry.java:118)
Caused by: oracle.net.ns.NetException: The Network Adapter could not establish the connection
at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:392)
at oracle.net.resolver.AddrResolution.resolveAndExecute(AddrResolution.java:434)
at oracle.net.ns.NSProtocol.establishConnection(NSProtocol.java:687)
at oracle.net.ns.NSProtocol.connect(NSProtocol.java:247)
at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1102)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:320)
... 11 more
Caused by: java.net.UnknownHostException: null
at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
at java.net.InetAddress$2.lookupAllHostAddr(Unknown Source)
at java.net.InetAddress.getAddressesFromNameService(Unknown Source)
at java.net.InetAddress.getAllByName0(Unknown Source)
at java.net.InetAddress.getAllByName(Unknown Source)
at java.net.InetAddress.getAllByName(Unknown Source)
at oracle.net.nt.TcpNTAdapter.connect(TcpNTAdapter.java:117)
at oracle.net.nt.ConnOption.connect(ConnOption.java:133)
at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:370)
... 16 more
The strange thing is, I can connect to the database from PL/SQL Developer, I can ping the remote host, and I can telnet to the remote host on port 1521.
Why would only Java appear to give an UnknownHostException, but I can connect and ping the host with other applications?
EDIT: I removed "hr/hr" from the connection string above. I have tried the connection as-is with it removed and still receive the same error. I've also tried changing the connection string to match the version morgano listed in his answer, with the same result. Finally, I tried to change the port number to a port I know it's not listening on, and it still receives the same error.
The connection string is of the correct format. The problem is that the host isn't set. It's null, or perhaps the string "null". There's simply no other way to generate the error message java.net.UnknownHostException: null
In your sample code, you write String host = "HOST_NAME";. This makes it look like in your real code you are assigning a constant string to the variable host. However, I'm going to stick my neck out and say that in your real code you are not doing this. You are looking up the host name from somewhere, this lookup fails for some reason, you don't check this, and hence you pass a null value into the connection string.
Your JDBC url is wrong, according to the documentation it should be something like:
jdbc:oracle:driver_type:[username/password]#//host_name:port_number/service_name
In your case your code would be something like:
import java.sql.*;
public class Main {
public Main () {
try {
String host = "HOST_NAME";
String port = "1521";
String service = "SERVICE_NAME";
String user = "SCHEMA_USER";
String password = "SCHEMA_PASSWORD";
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection connection = DriverManager.getConnection(
"jdbc:oracle:thin:#//" + host
+ ":" + port + "/" + service, user, password);
connection.close ();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main (String args) {
new Main ();
}
}

How to use PooledConnectionFactoryBean with ActiveMQ

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).

Categories

Resources