Connection to remote redis server is reset - redis

Trying the basic set operation on a redis server installed in red hat linux.
JedisPool pool = new JedisPool(new JedisPoolConfig(), HOST, PORT);
Jedis jedis = null;
try {
jedis = pool.getResource();
System.out.println(jedis.isConnected()); //prints true
jedis.set("status", "online"); //gets exception
} finally {
if (jedis != null) {
jedis.close();
}
}
pool.destroy();
Getting the following exception:
Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketException: Connection reset
at redis.clients.util.RedisInputStream.ensureFill(RedisInputStream.java:201)
at redis.clients.util.RedisInputStream.readByte(RedisInputStream.java:40)
at redis.clients.jedis.Protocol.process(Protocol.java:132)
at redis.clients.jedis.Protocol.read(Protocol.java:196)
at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:288)
at redis.clients.jedis.Connection.getStatusCodeReply(Connection.java:187)
at redis.clients.jedis.Jedis.set(Jedis.java:66)
at com.revechat.spring.redis_test.App.main(App.java:28)
Caused by: java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at redis.clients.util.RedisInputStream.ensureFill(RedisInputStream.java:195)
... 7 more
How to resolve the issue ?

I had a similar issue. Our production Redis required an encrypted connection over TLS, whereas our test system did not. In production therefore the java.net.SocketException: Connection reset appeared when we tried to use the Jedis connection.
To fix it, use
JedisPool pool = new JedisPool(new JedisPoolConfig(), HOST, PORT, true);
for connections that require TLS.

Related

RabbitMQ Java Client Connection Timeout

I am using the same client connection properties for Producer and Consumer. But while I am able to Produce messages to RabbitMQ, getting following error for Consumer side. I have read many solutions all say check out connection properties, firewall etc. But I have working producer.
Consumer side connection codes:
String userName = "myuser"; // not guest
String password = "mypassword"; // not guest
String virtualHost = "/";
String hostName = "one_of_rbmq_ip";
Integer portNumber = 5672;
ConnectionFactory factory = new ConnectionFactory();
// "guest"/"guest" by default, limited to localhost connections
factory.setUsername(userName);
factory.setPassword(password);
factory.setVirtualHost(virtualHost);
factory.setHost(hostName);
factory.setPort(portNumber);
Connection conn = factory.newConnection();
Error gives at last line (conn)
Exception in thread "main" java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:81)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:476)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:218)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:200)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:162)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:394)
at java.net.Socket.connect(Socket.java:606)
at com.rabbitmq.client.impl.SocketFrameHandlerFactory.create(SocketFrameHandlerFactory.java:60)
at com.rabbitmq.client.impl.recovery.RecoveryAwareAMQConnectionFactory.newConnection(RecoveryAwareAMQConnectionFactory.java:63)
at com.rabbitmq.client.impl.recovery.AutorecoveringConnection.init(AutorecoveringConnection.java:160)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1216)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1173)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1131)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1294)
at Condumer2.main(Condumer2.java:25)
I have changed my code like following it worked
Address[] addrArr = new Address[]{ new Address("10.28.94.117", 5672)
, new Address("10.28.94.118", 5672)
, new Address("10.28.94.119", 5672)
};
ConnectionFactory factory = new ConnectionFactory();
factory.setUsername("rabbitmq");
factory.setPassword("qa");
Connection connection = factory.newConnection(addrArr);
Channel channel = connection.createChannel();

Want to publish to message queue on Remote machine

I want to publish the message pojo from springboot application running in my local to message queue resides on one of the remote machine, but it is giving Authentication failure exception. I am able to publish the same object to message queue in my local.
I am using Springbatch to read, process and Write to message queue. I have used AmqpItemWriter and trying to publish to Message queue. Its worked well and published the message to message queue in my local. When i changed the property spring.rabbitmq.host from 'localhost' to 'xxx.xx.xx.xxx' remote machine came up with run time exception stating Authentication failed.
Below is my Writer code where I have logic to publish to message queue.
#Component
public class Writer extends AmqpItemWriter<CsvWrapperPojo> {
#Autowired
#Qualifier("rabbitTemplate")
private RabbitTemplate rabbitTemplate;
public Writer(AmqpTemplate rabbitTemplate) {
super(rabbitTemplate);
// TODO Auto-generated constructor stub
}
/*#Autowired
private CSVPostProcess csvPostProcess;*/
#Override
public void write(final List<? extends CsvWrapperPojo> items) throws Exception {
// TODO Auto-generated method stub
for(CsvWrapperPojo item : items){
for(CSVPojo pojo :item.getGeneralPojoList()){
rabbitTemplate.convertAndSend("spring-boot-rabbitmq-BulkSolve.async_BulkSolve_Msg", "BulkSolve_GeneralrequestQueue", pojo);
}
for(CSVPojo pojoSummary : item.getSummaryPojoList()){
rabbitTemplate.convertAndSend("spring-boot-rabbitmq-BulkSolve_summary.async_BulkSolve_Msg", "BulkSolve_SummaryrequestQueue", pojoSummary);
}
}
}
}
Below is application.properties
server.port=9060
spring.rabbitmq.dynamic=true
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.rabbitmq.host=xxx.xx.xx.xxx(Remote machine IP)
spring.batch.job.enabled=false
I have Rabbitmq installed and same queue is available on Remote machine too. But It seems authentication is not happening here. Below is my exception stack trace. Can any one suggest what need to do to publish message to remote machine.
java.net.SocketException: Socket Closed
at java.net.SocketInputStream.socketRead0(Native Method) ~[na:1.8.0_131]
at java.net.SocketInputStream.socketRead(SocketInputStream.java:116) ~[na:1.8.0_131]
at java.net.SocketInputStream.read(SocketInputStream.java:171) ~[na:1.8.0_131]
at java.net.SocketInputStream.read(SocketInputStream.java:141) ~[na:1.8.0_131]
at java.io.BufferedInputStream.fill(BufferedInputStream.java:246) ~[na:1.8.0_131]
at java.io.BufferedInputStream.read(BufferedInputStream.java:265) ~[na:1.8.0_131]
at java.io.DataInputStream.readUnsignedByte(DataInputStream.java:288) ~[na:1.8.0_131]
at com.rabbitmq.client.impl.Frame.readFrom(Frame.java:91) ~[amqp-client-4.0.3.jar!/:4.0.3]
at com.rabbitmq.client.impl.SocketFrameHandler.readFrame(SocketFrameHandler.java:164) ~[amqp-client-4.0.3.jar!/:4.0.3]
at com.rabbitmq.client.impl.AMQConnection$MainLoop.run(AMQConnection.java:571) ~[amqp-client-4.0.3.jar!/:4.0.3]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_131]
2019-05-16 15:56:41.683 ERROR 696 --- [nio-9060-exec-1] o.s.batch.core.step.AbstractStep : Encountered an error executing step ETL-CSV in job ETL
org.springframework.amqp.AmqpAuthenticationException: com.rabbitmq.client.AuthenticationFailureException: ACCESS_REFUSED - Login was refused using authentication mechanism PLAIN. For details see the broker logfile.
at org.springframework.amqp.rabbit.support.RabbitExceptionTranslator.convertRabbitAccessException(RabbitExceptionTranslator.java:65) ~[spring-rabbit-1.7.4.RELEASE.jar!/:na]
at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.createBareConnection(AbstractConnectionFactory.java:368) ~[spring-rabbit-1.7.4.RELEASE.jar!/:na]
at org.springframework.amqp.rabbit.connection.CachingConnectionFactory.createConnection(CachingConnectionFactory.java:573) ~[spring-rabbit-1.7.4.RELEASE.jar!/:na]
at org.springframework.amqp.rabbit.core.RabbitTemplate.doExecute(RabbitTemplate.java:1430) ~[spring-rabbit-1.7.4.RELEASE.jar!/:na]
at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:1411) ~[spring-rabbit-1.7.4.RELEASE.jar!/:na]
at org.springframework.amqp.rabbit.core.RabbitTemplate.send(RabbitTemplate.java:712) ~[spring-rabbit-1.7.4.RELEASE.jar!/:na]
at org.springframework.amqp.rabbit.core.RabbitTemplate.convertAndSend(RabbitTemplate.java:780) ~[spring-rabbit-1.7.4.RELEASE.jar!/:na]
at org.springframework.amqp.rabbit.core.RabbitTemplate.convertAndSend(RabbitTemplate.java:776) ~[spring-rabbit-1.7.4.RELEASE.jar!/:na]
at com.comcast.FileProcess.Batch.Writer.write(Writer.java:47) ~[classes!/:0.0.1-SNAPSHOT]
at org.springframework.batch.core.step.item.SimpleChunkProcessor.writeItems(SimpleChunkProcessor.java:175) ~[spring-batch-core-3.0.8.RELEASE.jar!/:3.0.8.RELEASE]
at org.springframework.batch.core.step.item.SimpleChunkProcessor.doWrite(SimpleChunkProcessor.java:151) ~[spring-batch-core-3.0.8.RELEASE.jar!/:3.0.8.RELEASE]
at org.springframework.batch.core.step.item.SimpleChunkProcessor.write(SimpleChunkProcessor.java:274) ~[spring-batch-core-3.0.8.RELEASE.jar!/:3.0.8.RELEASE]
at org.springframework.batch.core.step.item.SimpleChunkProcessor.process(SimpleChunkProcessor.java:199) ~[spring-batch-core-3.0.8.RELEASE.jar!/:3.0.8.RELEASE]
at org.springframework.batch.core.step.item.ChunkOrientedTasklet.execute(ChunkOrientedTasklet.java:75) ~[spring-batch-core-3.0.8.RELEASE.jar!/:3.0.8.RELEASE]
at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:406) ~[spring-batch-core-3.0.8.RELEASE.jar!/:3.0.8.RELEASE]
at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:330) ~[spring-batch-core-3.0.8.RELEASE.jar!/:3.0.8.RELEASE]
guest/guest it is not allowed remotely by default.
See "guest" user can only connect from localhost.
By default, the guest user is prohibited from connecting from remote hosts; it can only connect over a loopback interface (i.e. localhost). This applies to connections regardless of the protocol. Any other users will not (by default) be restricted in this way.
It goes on to explain how to reconfigure the broker if you really want to allow it.

FCM/GCM App Server cannot connect to FCM/GCM CCS using SSLSocket and SMACK library

I'm writing a Firebase Cloud Messaing (FCM) App server program, and encountered a connection problem when I try to connect to FCM server via XMPP connection, using SMACK library. The program fails to be connected to the FCM server (fcm-xmpp.googleapis.com:5236).
Following an example,
https://github.com/carlosCharz/fcmxmppserverv2/blob/master/src/main/java/com/wedevol/xmpp/server/CcsClient.java,
I try to connect to the FCM server using SSL socket. The source codes are as follows.
XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder ();
configBuilder.setUsernameAndPassword (senderId, serverKey);
configBuilder.setServiceName (CCMServiceName);
configBuilder.setHost (CCMServer);
configBuilder.setPort (CCMServerPort);
configBuilder.setSocketFactory (SSLSocketFactory.getDefault ());
configBuilder.setSecurityMode (SecurityMode.ifpossible);
configBuilder.setSendPresence (true);
configBuilder.setDebuggerEnabled(true);
SASLAuthentication.unBlacklistSASLMechanism("PLAIN");
//Step 2. Generate a connection resource with CCM Server
connection = new XMPPTCPConnection (configBuilder.build ());
//Step 3. Connect
try {
systemLogStream.println ("Try to connect XMPP end-point: " + CCMServer );
connection.connect();
} catch (SmackException smackEx) {
systemLogStream.println ("SMACK Exception: " + smackEx.getMessage());
System.exit(999);
} catch (XMPPException xmppEx) {
systemLogStream.println ("XMPP Exception: ");
} catch (IOException ioEx) {
systemLogStream.println ("IO Exception: ");
}
However, it always fails to connect to FCM server generating following exception.
SMACK Exception: No response received within reply timeout. Timeout was 5000ms (~5s). Used filter: No filter used or filter was 'null'.
To figure out the reason, I captured tcpdumps and it looks like SSLSocket does not send Clienthello message which is the first step to TLS handshake. Following is the result from Wireshark.
Wireshark capture result
I hope that someone nice gives a comment or an advise how to resolve this problem.
Thank you.

Cannot connect to redis using jedis

Redis version: 3.2.0
Jedis version: 2.8.1
Below is my java code for connecting to redis:
public class TestRedis {
public static void main(String[] args) {
String host = args[0];
int port = Integer.parseInt(args[1]);
try (Jedis jedis = new Jedis(host, port)) {
System.out.println("Connected to jedis " + jedis.ping());
} catch(Exception e){
e.printStackTrace();
}
}
}
I am running this program in the machine where redis is installed. This machine's ip address is 192.168.1.57
If I provide host="localhost" and port = "6379" as arguments, connection with redis successfully established.
However, If I give host="192.168.1.57" and port = "6379" in arguments, I end up with below exception:
redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused
at redis.clients.jedis.Connection.connect(Connection.java:164)
at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:80)
at redis.clients.jedis.Connection.sendCommand(Connection.java:100)
at redis.clients.jedis.Connection.sendCommand(Connection.java:95)
at redis.clients.jedis.BinaryClient.ping(BinaryClient.java:93)
at redis.clients.jedis.BinaryJedis.ping(BinaryJedis.java:105)
at TestRedis.main(TestRedis.java:14)
Caused by: java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at redis.clients.jedis.Connection.connect(Connection.java:158)
... 6 more
Please help...
There are a few settings that would affect this: bind and protected-mode. They work together to provide a baseline of security with new installs.
Find the following in your redis.conf file and comment it out:
bind 127.0.0.1
By adding a # in front of it:
# bind 127.0.0.1
Or, if you would rather not comment it out, you can also add the IP of your eth0/em1 interface to it, like this:
bind 127.0.0.1 192.168.1.57
Also, unless you're using password security, you'll also have to turn off protected mode by changing:
protected-mode yes
To:
protected-mode no
Make sure that you read the relevant documentation and understand the security implications of both of these changes.
After making these changes, restart redis.

Accessing RabbitMQ from different clusters/machines

I have a RabbitMQ instance deployed on a google cloud engine. I also have a hadoop instance deployed on a different google cloud engine but still in the same application. I am trying to connect to the RabbitMQ queue instance from the hadoop clusters but with no success.
I have a java application that should push items on the RabbitMQ queue and then receive them in the same application. The following is the connection java code:
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("130.211.112.37:5672");
try {
connection = factory.newConnection();
channel = connection.createChannel();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
but i get the following result:
java.net.UnknownHostException: 130.211.112.37:5672
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:178)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at com.rabbitmq.client.impl.FrameHandlerFactory.create(FrameHandlerFactory.java:32)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:615)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:639)
at de.unibonn.iai.eis.luzzu.io.impl.SparkStreamProcessorObserver.<clinit>(SparkStreamProcessorObserver.java:157)
at de.unibonn.iai.eis.luzzu.evaluation.Main.main(Main.java:87)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.spark.deploy.SparkSubmit$.launch(SparkSubmit.scala:328)
at org.apache.spark.deploy.SparkSubmit$.main(SparkSubmit.scala:75)
at org.apache.spark.deploy.SparkSubmit.main(SparkSubmit.scala)
I tried opening port 5672 on google cloud firewall. Does anyone has some pointers to the solution please?
Best
Jeremy
As wrote to the comment:
ConnectionFactory factory = new ConnectionFactory();
//factory.setHost("130.211.112.37:5672"); <----- sethost accepts only the host!
factory.setHost("130.211.112.37");
factory.setPort(5672);
try {
connection = factory.newConnection();
channel = connection.createChannel();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
By default the port is 5672, so setPort it is not necessary.
You have to use setPort only if you change the default port.
As explained here: https://www.rabbitmq.com/api-guide.html you need to call setHost and setPort to create a connection. In your app you are passing the host and port together on the same line.