David,
I have learned a lot from the documentation as you suggested. I was successful in setting up a Maven environment for Elasticsearch. I tried both the low-level and high-level poms, and successfully created a .jar file to use in my Tomcat application. I can call methods in this jar file to connect to Elastic, but once I get to the RestClient.builder() in either the low or high level client calls, the code simply hangs up.
pom file:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd ">
<modelVersion>4.0.0</modelVersion>
<artifactId>N2NElasticsearch</artifactId>
<groupId>com.nodonomy.elasticsearch</groupId>
<version>1.0.0</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.1.0</version>
</dependency>
<dependency>
<groupId>org.eclipse.ecf</groupId>
<artifactId>org.apache.log4j</artifactId>
<version>1.2.15.v201012070815</version>
</dependency>
<dependency>
<groupId>org.apache.clerezza.ext</groupId>
<artifactId>org.json.simple</artifactId>
<version>0.4</version>
</dependency>
</dependencies>
</project>
And my Java class:
package com.mycompany.elasticsearch;
import org.apache.log4j.Logger;
import org.apache.http.HttpHost;
import org.elasticsearch.client.*;
import org.json.simple.JSONObject;
public class N2NESElastic {
private Logger logger = null;
public N2NESElastic() {
logger = Logger.getLogger(com.mycompany.elasticsearch.N2NESElastic.class);
logger.info("[SERVER] N2NESElastic(). Inside the constructor...");
}
private static final String HOST = "localhost"; //"127.0.0.1";
private static final int PORT_ONE = 9200;
private static final int PORT_TWO = 9201;
private static final String SCHEME = "http";
private RestClient restClient = null;
private RestHighLevelClient client = null;
@SuppressWarnings("unchecked")
public synchronized JSONObject processRequest(JSONObject jsonReqAsObject, String command) {
logger.info("[N2NESElastic] Command: " + command);
JSONObject json = null;
String message = "command: " + command;
// - - - - - - - - - - - - - - - - - - - -
// O P E N
// - - - - - - - - - - - - - - - - - - - -
logger.info("Calling makeConnection()...");
this.makeConnection();
logger.info("Connection established...");
// - - - - - - - - - - - - - - - - - - - -
// C L O S E
// - - - - - - - - - - - - - - - - - - - -
try {
logger.info("Calling closeConnection()...");
this.closeConnection();
logger.info("Connection closed...");
}
catch (Exception e) {
logger.error("**xERROR> " + e.toString());
}
json.put("message", message);
return json;
}
private synchronized void makeConnection() {
logger.info("Making connection...");
logger.info("Creating hostPort1...");
HttpHost hostPort1 = new HttpHost("localhost", 9200, "http");
logger.info("Creating hostPort2...");
HttpHost hostPort2 = new HttpHost("localhost", 9201, "http");
//logger.info("Calling restClient builder...");
//RestClient.builder(hostPort1, hostPort2).build(); // LOW LEVEL: HANGS
logger.info("Calling high-level-rest builder..."); // HIGH LEVEL: ALSO HANGS
client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("localhost", 9200, "http"),
new HttpHost("localhost", 9201, "http")));
logger.info("Done building..."); // NEVER GET TO THIS LINE
}
private synchronized void closeConnection() {
try {
logger.info("Closing connection...");
restClient.close();
}
catch (Exception e) {
logger.error("**ERROR> " + e.toString());
}
}
}
Any help you can offer will be appreciated.
In my Eclipse project I have the following external .jar files from Elastic:
elasticsearch-7.1.0.jar
elasticsearch-core-7.1.0.jar
elasticsearch-rest-client-7.1.0.jar
elasticsearch-rest-high-level-client-7.1.0.jar
- David