Skip to content

Commit 9d8e085

Browse files
committed
code cleanup post elastic8 migration
1 parent a9096b9 commit 9d8e085

File tree

3 files changed

+48
-57
lines changed

3 files changed

+48
-57
lines changed

services/alarm-logger/pom.xml

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
<version>4.6.10-SNAPSHOT</version>
77
</parent>
88
<properties>
9-
<spring.boot-version>2.1.5.RELEASE</spring.boot-version>
109
<java.version>1.11</java.version>
10+
<spring.boot-version>2.6.7</spring.boot-version>
11+
<elasticsearch.version>8.2.0</elasticsearch.version>
1112
</properties>
1213
<artifactId>service-alarm-logger</artifactId>
1314

@@ -79,13 +80,23 @@
7980
<dependency>
8081
<groupId>com.fasterxml.jackson.core</groupId>
8182
<artifactId>jackson-core</artifactId>
82-
<version>2.13.2</version>
83+
<version>2.12.3</version>
84+
</dependency>
85+
<dependency>
86+
<groupId>jakarta.json</groupId>
87+
<artifactId>jakarta.json-api</artifactId>
88+
<version>2.0.1</version>
8389
</dependency>
8490
<dependency>
8591
<groupId>com.fasterxml.jackson.core</groupId>
8692
<artifactId>jackson-annotations</artifactId>
8793
<version>2.13.2</version>
8894
</dependency>
95+
<dependency>
96+
<groupId>com.fasterxml.jackson.datatype</groupId>
97+
<artifactId>jackson-datatype-jsr310</artifactId>
98+
<version>2.13.2</version>
99+
</dependency>
89100
<dependency>
90101
<groupId>org.phoebus</groupId>
91102
<artifactId>core-util</artifactId>
@@ -111,6 +122,22 @@
111122
<version>1.7.28</version>
112123
</dependency>
113124

125+
<dependency>
126+
<groupId>org.springframework</groupId>
127+
<artifactId>spring-test</artifactId>
128+
<scope>test</scope>
129+
</dependency>
130+
<dependency>
131+
<groupId>org.springframework.boot</groupId>
132+
<artifactId>spring-boot-starter-test</artifactId>
133+
<scope>test</scope>
134+
</dependency>
135+
<dependency>
136+
<groupId>junit</groupId>
137+
<artifactId>junit</artifactId>
138+
<scope>test</scope>
139+
</dependency>
140+
114141

115142
</dependencies>
116143
<build>

services/alarm-logger/src/main/java/org/phoebus/alarm/logging/ElasticClientHelper.java

Lines changed: 14 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
import co.elastic.clients.elasticsearch.core.BulkRequest;
1010
import co.elastic.clients.elasticsearch.core.BulkResponse;
1111
import co.elastic.clients.elasticsearch.core.IndexResponse;
12+
import co.elastic.clients.transport.ElasticsearchTransport;
1213
import co.elastic.clients.transport.rest_client.RestClientTransport;
1314
import co.elastic.clients.elasticsearch.indices.*;
1415
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
1516
import com.fasterxml.jackson.databind.ObjectMapper;
17+
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
1618
import org.apache.http.HttpHost;
1719
import co.elastic.clients.elasticsearch.core.IndexRequest;
1820
import org.elasticsearch.client.RestClient;
@@ -45,7 +47,7 @@ public class ElasticClientHelper {
4547

4648
private static RestClient restClient;
4749

48-
private static RestClientTransport transport;
50+
private static ElasticsearchTransport transport;
4951

5052
private static ElasticsearchClient client;
5153
private static ElasticClientHelper instance;
@@ -60,6 +62,8 @@ public class ElasticClientHelper {
6062
// State messages to be indexed
6163
BlockingQueue<SimpleImmutableEntry<String,AlarmConfigMessage>> configMessagedQueue = new LinkedBlockingDeque<>();
6264

65+
private final ObjectMapper mapper = new ObjectMapper();
66+
6367
private ElasticClientHelper() {
6468
try {
6569
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
@@ -75,9 +79,16 @@ private ElasticClientHelper() {
7579
}
7680
}
7781
}));
82+
83+
// Create the low-level client
7884
restClient = RestClient.builder(
79-
new HttpHost(new HttpHost(props.getProperty("es_host"),Integer.parseInt(props.getProperty("es_port"))))).build();
80-
transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
85+
new HttpHost(props.getProperty("es_host"),Integer.parseInt(props.getProperty("es_port")))).build();
86+
87+
mapper.registerModule(new JavaTimeModule());
88+
transport = new RestClientTransport(
89+
restClient,
90+
new JacksonJsonpMapper(mapper)
91+
);
8192
client = new ElasticsearchClient(transport);
8293
if (props.getProperty("es_sniff").equals("true")) {
8394
sniffer = Sniffer.builder(restClient).build();
@@ -114,38 +125,6 @@ public ElasticsearchClient getClient() {
114125
return client;
115126
}
116127

117-
/**
118-
* Check if an index exists with the given name
119-
* Note: this is an synchronous call
120-
*
121-
* @param indexName elastic index name / pattern
122-
* @return true if index exists
123-
*/
124-
public boolean indexExists(String indexName) {
125-
ExistsRequest xRequest = new ExistsRequest.Builder()
126-
.index(indexName.toLowerCase())
127-
.build();
128-
try {
129-
return client.indices().exists(xRequest).value();
130-
} catch (IOException e) {
131-
logger.log(Level.WARNING, "Failed to query elastic", e);
132-
return false;
133-
}
134-
}
135-
136-
public void indexAlarmStateDocument(String indexName, AlarmStateMessage alarmStateMessage) {
137-
IndexRequest<AlarmStateMessage> indexRequest = new IndexRequest.Builder<AlarmStateMessage>()
138-
.index(indexName.toLowerCase())
139-
.document(alarmStateMessage)
140-
.build();
141-
try {
142-
IndexResponse idxResponse = client.index(indexRequest);
143-
} catch (IOException e) {
144-
logger.log(Level.SEVERE, "failed to log message " + alarmStateMessage + " to index " + indexName, e);
145-
}
146-
}
147-
148-
149128
public void indexAlarmStateDocuments(String indexName, AlarmStateMessage alarmStateMessage) {
150129
try {
151130
stateMessagedQueue.put(new SimpleImmutableEntry<>(indexName,alarmStateMessage));
@@ -168,20 +147,6 @@ public boolean indexAlarmCmdDocument(String indexName, AlarmCommandMessage alarm
168147
}
169148
}
170149

171-
public boolean indexAlarmConfigDocument(String indexName, AlarmConfigMessage alarmConfigMessage) {
172-
IndexRequest<AlarmConfigMessage> indexRequest = new IndexRequest.Builder<AlarmConfigMessage>()
173-
.index(indexName.toLowerCase())
174-
.document(alarmConfigMessage)
175-
.build();
176-
try {
177-
IndexResponse indexResponse = client.index(indexRequest);
178-
return indexResponse.result().equals(Result.Created);
179-
} catch (IOException e) {
180-
logger.log(Level.SEVERE, "failed to log message " + alarmConfigMessage + " to index " + indexName, e);
181-
return false;
182-
}
183-
}
184-
185150
public void indexAlarmConfigDocuments(String indexName, AlarmConfigMessage alarmConfigMessage) {
186151
try {
187152
configMessagedQueue.put(new SimpleImmutableEntry<>(indexName,alarmConfigMessage));
@@ -257,7 +222,6 @@ public void initializeIndices() throws IOException {
257222
boolean exists = client.indices().existsTemplate(request).value();
258223

259224
if(!exists) {
260-
ObjectMapper mapper = new ObjectMapper();
261225
InputStream is = ElasticClientHelper.class.getResourceAsStream("/alarms_state_template.json");
262226
PutIndexTemplateRequest templateRequest = new PutIndexTemplateRequest.Builder()
263227
.name(ALARM_STATE_TEMPLATE)
@@ -277,7 +241,6 @@ public void initializeIndices() throws IOException {
277241
exists = client.indices().existsTemplate(request).value();
278242

279243
if(!exists) {
280-
ObjectMapper mapper = new ObjectMapper();
281244
InputStream is = ElasticClientHelper.class.getResourceAsStream("/alarms_cmd_template.json");
282245
PutIndexTemplateRequest templateRequest = new PutIndexTemplateRequest.Builder()
283246
.name(ALARM_CMD_TEMPLATE)
@@ -297,7 +260,6 @@ public void initializeIndices() throws IOException {
297260
exists = client.indices().existsTemplate(request).value();
298261

299262
if(!exists) {
300-
ObjectMapper mapper = new ObjectMapper();
301263
InputStream is = ElasticClientHelper.class.getResourceAsStream("/alarms_cmd_template.json");
302264
PutIndexTemplateRequest templateRequest = new PutIndexTemplateRequest.Builder()
303265
.name(ALARM_CONFIG_TEMPLATE)

services/alarm-logger/src/test/java/org/phoebus/alarm/logging/messages/MapperTest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package org.phoebus.alarm.logging.messages;
22

3-
import static org.junit.Assert.assertArrayEquals;
4-
import static org.junit.Assert.assertEquals;
5-
import static org.junit.Assert.fail;
3+
64

75
import java.io.IOException;
86
import java.util.HashMap;
@@ -15,6 +13,10 @@
1513
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
1614
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
1715

16+
import static org.junit.Assert.assertArrayEquals;
17+
import static org.junit.Assert.assertEquals;
18+
import static org.junit.Assert.fail;
19+
1820
public class MapperTest {
1921

2022
@Test

0 commit comments

Comments
 (0)