Commit 0dc6fd3b authored by Vishal Lal's avatar Vishal Lal Committed by Vishal Lal

Inital checkin for health endpoint. Add rabbitmq timeout

parent b1ba3a74
......@@ -20,7 +20,8 @@ public class RabbitMqConfiguration
public ConnectionFactory connectionFactory()
{
CachingConnectionFactory connectionFactory = new CachingConnectionFactory("rabbitmq");
// CachingConnectionFactory connectionFactory = new CachingConnectionFactory("192.168.99.104");
connectionFactory.setCloseTimeout(5000);
connectionFactory.setConnectionTimeout(5000);
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");
return connectionFactory;
......
package works.weave.socks.queuemaster.controllers;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.rabbit.core.ChannelCallback;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import works.weave.socks.queuemaster.entities.HealthCheck;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
public class HealthCheckController {
@Autowired
RabbitTemplate rabbitTemplate;
@ResponseStatus(HttpStatus.OK)
@RequestMapping(method = RequestMethod.GET, path = "/health")
public
@ResponseBody
Map<String, List<HealthCheck>> getHealth() {
Map<String, List<HealthCheck>> map = new HashMap<String, List<HealthCheck>>();
List<HealthCheck> healthChecks = new ArrayList<HealthCheck>();
Date dateNow = Calendar.getInstance().getTime();
HealthCheck app = new HealthCheck("queue-master", "OK", dateNow);
HealthCheck rabbitmq = new HealthCheck("queue-master-rabbitmq", "OK", dateNow);
try {
this.rabbitTemplate.execute(new ChannelCallback<String>() {
@Override
public String doInRabbit(Channel channel) throws Exception {
Map<String, Object> serverProperties = channel.getConnection().getServerProperties();
return serverProperties.get("version").toString();
}
});
} catch ( AmqpException e ) {
rabbitmq.setStatus("err");
}
healthChecks.add(app);
healthChecks.add(rabbitmq);
map.put("health", healthChecks);
return map;
}
}
package works.weave.socks.queuemaster.entities;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonFormat;
import java.util.Calendar;
import java.util.Date;
@JsonIgnoreProperties(ignoreUnknown = true)
public class HealthCheck {
private String service;
private String status;
@JsonFormat(pattern="yyyy-MM-dd'T'HH:mm:ss.SSSXXX")
private Date date = Calendar.getInstance().getTime();
public HealthCheck() {
}
public HealthCheck(String service, String status, Date date) {
this.service = service;
this.status = status;
this.date = date;
}
@Override
public String toString() {
return "HealthCheck{" +
"service='" + service + '\'' +
", status='" + status + '\'' +
", date='" + date +
'}';
}
public String getService() {
return service;
}
public void setService(String service) {
this.service = service;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
server.port=${port:8080}
\ No newline at end of file
server.port=${port:8080}
endpoints.health.enabled=false
package works.weave.socks.queuemaster.controllers;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;
import works.weave.socks.queuemaster.entities.HealthCheck;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.*;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class ITHealthCheckController {
@Autowired
private HealthCheckController healthCheckController;
@Test
public void getHealthCheck() throws Exception {
Map<String, List<HealthCheck>> healthChecks = healthCheckController.getHealth();
assertThat(healthChecks.get("health").size(), is(equalTo(2)));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment