Check if node is listening before sending a request

Hi,

I'm using Elastic Search with Scala and the liftweb framework and on
several areas I have something like this:

client.prepareIndex("inventory", "main", c.id.toString)
.setSource(jsonBuilder()
.startObject()
.field("id", c.id.is)
.field("part_number", c.part_number.toString)
.field("category", ~(c.category.obj map (.category_name.toString)))
.field("subcategory", ~(c.subcategory.obj map
(
.subcategory_name.toString)))
.endObject()
)
.execute()
.actionGet()

and client is:

val client: Box[Client] = {
info("Calling client!")
new TransportClient().addTransportAddress(
new InetSocketTransportAddress("192.168.0.100", 9300)
)
}

pretty much just a translation from java to scala. The problem I have
is that is my elastic search instance is down, and I execute the code
above, I get an exception on my logs, something along the lines:

org.elasticsearch.transport.ConnectTransportException:
[][inet[/192.168.0.100:9300]] connect_timeout[30s]
at org.elasticsearch.transport.netty.NettyTransport.connectToChannels(NettyTransport.java:562)
~[elasticsearch-0.18.2.jar:na]
at org.elasticsearch.transport.netty.NettyTransport.connectToNode(NettyTransport.java:505)
~[elasticsearch-0.18.2.jar:na]
at org.elasticsearch.transport.netty.NettyTransport.connectToNode(NettyTransport.java:484)
~[elasticsearch-0.18.2.jar:na]
at org.elasticsearch.transport.TransportService.connectToNode(TransportService.java:124)
~[elasticsearch-0.18.2.jar:na]
at org.elasticsearch.client.transport.TransportClientNodesService$SimpleNodeSampler.sample(TransportClientNodesService.java:262)
~[elasticsearch-0.18.2.jar:na]
at org.elasticsearch.client.transport.TransportClientNodesService$ScheduledNodeSampler.run(TransportClientNodesService.java:245)
[elasticsearch-0.18.2.jar:na]
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
[na:1.6.0_25]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
[na:1.6.0_25]
at java.lang.Thread.run(Thread.java:662) [na:1.6.0_25]
Caused by: java.net.ConnectException: Connection refused
at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method) ~[na:1.6.0_25]
at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:567)
~[na:1.6.0_25]
at org.elasticsearch.common.netty.channel.socket.nio.NioClientSocketPipelineSink$Boss.connect(NioClientSocketPipelineSink.java:384)
~[elasticsearch-0.18.2.jar:na]
at org.elasticsearch.common.netty.channel.socket.nio.NioClientSocketPipelineSink$Boss.processSelectedKeys(NioClientSocketPipelineSink.java:354)
~[elasticsearch-0.18.2.jar:na]
at org.elasticsearch.common.netty.channel.socket.nio.NioClientSocketPipelineSink$Boss.run(NioClientSocketPipelineSink.java:276)
~[elasticsearch-0.18.2.jar:na]
at org.elasticsearch.common.netty.util.ThreadRenamingRunnable.run(ThreadRenamingRunnable.java:108)
~[elasticsearch-0.18.2.jar:na]
at org.elasticsearch.common.netty.util.internal.DeadLockProofWorker$1.run(DeadLockProofWorker.java:44)
~[elasticsearch-0.18.2.jar:na]
... 3 common frames omitted

While my app is running I handle this nicely and I show a message on
the browser telling the user what to do, but during unit test (ES is
not running) I want to first check if the port is not listening, and
if that is the case, just not even try to run:
...
.execute()
.actionGet()

Is there an API to do this or should I do it in plain java?

Thanks

Diego

P.S. The integration with Scala and Lift is working out really smooth :slight_smile:

--
Diego Medina
Web Developer
diego@fmpwizard.com
http://www.fmpwizard.com

TransportClient has a method called connectedNodes that returns the nodes
that the client is connected to.

On Wed, Nov 23, 2011 at 10:09 PM, Diego Medina diego@fmpwizard.com wrote:

Hi,

I'm using Elastic Search with Scala and the liftweb framework and on
several areas I have something like this:

client.prepareIndex("inventory", "main", c.id.toString)
.setSource(jsonBuilder()
.startObject()
.field("id", c.id.is)
.field("part_number", c.part_number.toString)
.field("category", ~(c.category.obj map (.category_name.toString)))
.field("subcategory", ~(c.subcategory.obj map
(
.subcategory_name.toString)))
.endObject()
)
.execute()
.actionGet()

and client is:

val client: Box[Client] = {
info("Calling client!")
new TransportClient().addTransportAddress(
new InetSocketTransportAddress("192.168.0.100", 9300)
)
}

pretty much just a translation from java to scala. The problem I have
is that is my Elasticsearch instance is down, and I execute the code
above, I get an exception on my logs, something along the lines:

org.elasticsearch.transport.ConnectTransportException:
[inet[/192.168.0.100:9300]] connect_timeout[30s]
at
org.elasticsearch.transport.netty.NettyTransport.connectToChannels(NettyTransport.java:562)
~[elasticsearch-0.18.2.jar:na]
at
org.elasticsearch.transport.netty.NettyTransport.connectToNode(NettyTransport.java:505)
~[elasticsearch-0.18.2.jar:na]
at
org.elasticsearch.transport.netty.NettyTransport.connectToNode(NettyTransport.java:484)
~[elasticsearch-0.18.2.jar:na]
at
org.elasticsearch.transport.TransportService.connectToNode(TransportService.java:124)
~[elasticsearch-0.18.2.jar:na]
at
org.elasticsearch.client.transport.TransportClientNodesService$SimpleNodeSampler.sample(TransportClientNodesService.java:262)
~[elasticsearch-0.18.2.jar:na]
at
org.elasticsearch.client.transport.TransportClientNodesService$ScheduledNodeSampler.run(TransportClientNodesService.java:245)
[elasticsearch-0.18.2.jar:na]
at
java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
[na:1.6.0_25]
at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
[na:1.6.0_25]
at java.lang.Thread.run(Thread.java:662) [na:1.6.0_25]
Caused by: java.net.ConnectException: Connection refused
at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
~[na:1.6.0_25]
at
sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:567)
~[na:1.6.0_25]
at
org.elasticsearch.common.netty.channel.socket.nio.NioClientSocketPipelineSink$Boss.connect(NioClientSocketPipelineSink.java:384)
~[elasticsearch-0.18.2.jar:na]
at
org.elasticsearch.common.netty.channel.socket.nio.NioClientSocketPipelineSink$Boss.processSelectedKeys(NioClientSocketPipelineSink.java:354)
~[elasticsearch-0.18.2.jar:na]
at
org.elasticsearch.common.netty.channel.socket.nio.NioClientSocketPipelineSink$Boss.run(NioClientSocketPipelineSink.java:276)
~[elasticsearch-0.18.2.jar:na]
at
org.elasticsearch.common.netty.util.ThreadRenamingRunnable.run(ThreadRenamingRunnable.java:108)
~[elasticsearch-0.18.2.jar:na]
at
org.elasticsearch.common.netty.util.internal.DeadLockProofWorker$1.run(DeadLockProofWorker.java:44)
~[elasticsearch-0.18.2.jar:na]
... 3 common frames omitted

While my app is running I handle this nicely and I show a message on
the browser telling the user what to do, but during unit test (ES is
not running) I want to first check if the port is not listening, and
if that is the case, just not even try to run:
...
.execute()
.actionGet()

Is there an API to do this or should I do it in plain java?

Thanks

Diego

P.S. The integration with Scala and Lift is working out really smooth :slight_smile:

--
Diego Medina
Web Developer
diego@fmpwizard.com
http://www.fmpwizard.com