I am trying to set up ElasticSearch with DockerComposeContainer. This works. But when I try to insert a document into ElasticSearch, I get org.apache.http.ConnectionClosedException: Connection closed
. It seems to me that the only way to avoid that error is to stop the program flow with a breakpoint and then run the command to insert a document in the debugging mode. I tried adding Thread.sleep(15000) before inserting the document, but it had no effect.
I am using java 1.8, MacBook Pro OS X version 10.11.6, 2.6 GHz Intel Core i5 processor, 8 GB 1600 MHz DDR3 memory
Help with this issue would be much appreciated.
import org.apache.http.HttpHost;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.junit.ClassRule;
import org.junit.Test;
import org.testcontainers.containers.DockerComposeContainer;
import java.io.File;
public class RepositoryTest {
@ClassRule
public static DockerComposeContainer elasticContainer =
new DockerComposeContainer(new File("docker-compose.yml"))
.withExposedService("elasticsearch_1", 9200)
.withExposedService("elasticsearch_1", 9300);
@Test
public void test() throws Exception {
RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200, "http")).build();
RestHighLevelClient client = new RestHighLevelClient(restClient);
IndexRequest indexRequest = new IndexRequest("posts", "doc", "1")
.source("user", "kimchy");
try {
client.index(indexRequest);
} catch (Exception e) {
System.out.println("");
}
}
}
docker-compose.yml:
version: "2"
services:
elasticsearch:
image: "docker.elastic.co/elasticsearch/elasticsearch:5.6.2"
environment:
network.host: "0.0.0.0"
xpack.security.enabled: "false"
transport.host: "0.0.0.0"
http.host: "0.0.0.0"
network.publish_host: "127.0.0.1"
transport.tcp.port: "9300"
discovery.zen.minimum_master_nodes: "1"
discovery.type: "single-node"
client.transport.sniff: "false"
ports:
- "9200:9200"
- "9300:9300"
The logs are available in the pastebin.