Skip to content
This repository was archived by the owner on Dec 5, 2023. It is now read-only.

Commit d731a4c

Browse files
Vishal LalVishal Lal
authored andcommitted
Inital checkin for health endpoint
1 parent b1ba3a7 commit d731a4c

File tree

4 files changed

+149
-1
lines changed

4 files changed

+149
-1
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package works.weave.socks.queuemaster.controllers;
2+
3+
import com.rabbitmq.client.Channel;
4+
import org.springframework.amqp.AmqpException;
5+
import org.springframework.amqp.rabbit.core.ChannelCallback;
6+
import org.springframework.amqp.rabbit.core.RabbitTemplate;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.http.HttpStatus;
10+
import org.springframework.web.bind.annotation.*;
11+
import works.weave.socks.queuemaster.entities.HealthCheck;
12+
13+
import java.util.ArrayList;
14+
import java.util.Calendar;
15+
import java.util.Date;
16+
import java.util.HashMap;
17+
import java.util.List;
18+
import java.util.Map;
19+
20+
@RestController
21+
public class HealthCheckController {
22+
23+
@Autowired
24+
RabbitTemplate rabbitTemplate;
25+
26+
@ResponseStatus(HttpStatus.OK)
27+
@RequestMapping(method = RequestMethod.GET, path = "/health")
28+
public
29+
@ResponseBody
30+
Map<String, List<HealthCheck>> getHealth() {
31+
Map<String, List<HealthCheck>> map = new HashMap<String, List<HealthCheck>>();
32+
List<HealthCheck> healthChecks = new ArrayList<HealthCheck>();
33+
Date dateNow = Calendar.getInstance().getTime();
34+
35+
HealthCheck app = new HealthCheck("queue-master", "OK", dateNow);
36+
HealthCheck rabbitmq = new HealthCheck("queue-master-rabbitmq", "OK", dateNow);
37+
38+
try {
39+
this.rabbitTemplate.execute(new ChannelCallback<String>() {
40+
@Override
41+
public String doInRabbit(Channel channel) throws Exception {
42+
Map<String, Object> serverProperties = channel.getConnection().getServerProperties();
43+
return serverProperties.get("version").toString();
44+
}
45+
});
46+
} catch ( AmqpException e ) {
47+
rabbitmq.setStatus("err");
48+
}
49+
50+
healthChecks.add(app);
51+
healthChecks.add(rabbitmq);
52+
53+
map.put("health", healthChecks);
54+
return map;
55+
}
56+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package works.weave.socks.queuemaster.entities;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
import com.fasterxml.jackson.annotation.JsonFormat;
5+
6+
import java.util.Calendar;
7+
import java.util.Date;
8+
9+
@JsonIgnoreProperties(ignoreUnknown = true)
10+
public class HealthCheck {
11+
private String service;
12+
private String status;
13+
14+
@JsonFormat(pattern="yyyy-MM-dd'T'HH:mm:ss.SSSXXX")
15+
private Date date = Calendar.getInstance().getTime();
16+
17+
public HealthCheck() {
18+
19+
}
20+
21+
public HealthCheck(String service, String status, Date date) {
22+
this.service = service;
23+
this.status = status;
24+
this.date = date;
25+
}
26+
27+
@Override
28+
public String toString() {
29+
return "HealthCheck{" +
30+
"service='" + service + '\'' +
31+
", status='" + status + '\'' +
32+
", date='" + date +
33+
'}';
34+
}
35+
36+
public String getService() {
37+
return service;
38+
}
39+
40+
public void setService(String service) {
41+
this.service = service;
42+
}
43+
44+
public String getStatus() {
45+
return status;
46+
}
47+
48+
public void setStatus(String status) {
49+
this.status = status;
50+
}
51+
52+
public Date getDate() {
53+
return date;
54+
}
55+
56+
public void setDate(Date date) {
57+
this.date = date;
58+
}
59+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
server.port=${port:8080}
1+
server.port=${port:8080}
2+
endpoints.health.enabled=false
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package works.weave.socks.queuemaster.controllers;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.boot.test.context.SpringBootTest;
7+
import org.springframework.boot.test.mock.mockito.MockBean;
8+
import org.springframework.test.context.junit4.SpringRunner;
9+
import works.weave.socks.queuemaster.entities.HealthCheck;
10+
11+
import static org.hamcrest.CoreMatchers.*;
12+
import static org.junit.Assert.assertThat;
13+
import static org.mockito.Mockito.*;
14+
15+
import java.util.Calendar;
16+
import java.util.HashMap;
17+
import java.util.Map;
18+
import java.util.List;
19+
20+
@RunWith(SpringRunner.class)
21+
@SpringBootTest
22+
public class ITHealthCheckController {
23+
24+
@Autowired
25+
private HealthCheckController healthCheckController;
26+
27+
@Test
28+
public void getHealthCheck() throws Exception {
29+
Map<String, List<HealthCheck>> healthChecks = healthCheckController.getHealth();
30+
assertThat(healthChecks.get("health").size(), is(equalTo(2)));
31+
}
32+
}

0 commit comments

Comments
 (0)