Search in sources :

Example 1 with OperationException

use of com.ecwid.consul.v1.OperationException in project spring-cloud-consul by spring-cloud.

the class ConsulInboundMessageProducer method getEvents.

// @Scheduled(fixedDelayString = "${spring.cloud.consul.binder.eventDelay:30000}")
public void getEvents() {
    try {
        List<Event> events = this.eventService.watch();
        for (Event event : events) {
            // Map<String, Object> headers = new HashMap<>();
            // headers.put(MessageHeaders.REPLY_CHANNEL, outputChannel.)
            String decoded = new String(decodeFromString(event.getPayload()));
            sendMessage(getMessageBuilderFactory().withPayload(decoded).build());
        }
    } catch (OperationException e) {
        if (logger.isErrorEnabled()) {
            logger.error("Error getting consul events: " + e);
        }
    } catch (Exception e) {
        if (logger.isErrorEnabled()) {
            logger.error("Error getting consul events: " + e.getMessage());
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Error getting consul events", e);
        }
    }
}
Also used : Event(com.ecwid.consul.v1.event.model.Event) Base64Utils.decodeFromString(org.springframework.util.Base64Utils.decodeFromString) OperationException(com.ecwid.consul.v1.OperationException) OperationException(com.ecwid.consul.v1.OperationException)

Example 2 with OperationException

use of com.ecwid.consul.v1.OperationException in project spring-cloud-consul by spring-cloud.

the class ConsulHeartbeatTaskTests method disableReRegistration.

@Test
public void disableReRegistration() {
    TtlScheduler ttlScheduler = new TtlScheduler(heartbeatProperties, discoveryProperties, consulClient, ReregistrationPredicate.DEFAULT);
    ConsulHeartbeatTask consulHeartbeatTask = new ConsulHeartbeatTask(serviceId, ttlScheduler);
    heartbeatProperties.setReregisterServiceOnFailure(false);
    NewService service = new NewService();
    service.setId(serviceId);
    ttlScheduler.add(service);
    OperationException operationException = new OperationException(500, "Internal Server Error", "CheckID \"service:service-A\" does not have associated TTL");
    given(consulClient.agentCheckPass("service:" + serviceId)).willThrow(operationException);
    assertThatThrownBy(consulHeartbeatTask::run).isSameAs(operationException);
}
Also used : ConsulHeartbeatTask(org.springframework.cloud.consul.discovery.TtlScheduler.ConsulHeartbeatTask) NewService(com.ecwid.consul.v1.agent.model.NewService) OperationException(com.ecwid.consul.v1.OperationException) Test(org.junit.Test)

Example 3 with OperationException

use of com.ecwid.consul.v1.OperationException in project spring-cloud-consul by spring-cloud.

the class ConsulHeartbeatTaskTests method enableReRegistrationWithCustomPredicate.

@Test
public void enableReRegistrationWithCustomPredicate() {
    TtlScheduler ttlScheduler = new TtlScheduler(heartbeatProperties, discoveryProperties, consulClient, e -> e.getStatusContent().endsWith("does not have associated TTL"));
    ConsulHeartbeatTask consulHeartbeatTask = new ConsulHeartbeatTask(serviceId, ttlScheduler);
    heartbeatProperties.setReregisterServiceOnFailure(true);
    NewService service = new NewService();
    service.setId(serviceId);
    ttlScheduler.add(service);
    given(consulClient.agentCheckPass("service:" + serviceId)).willThrow(new OperationException(400, "Internal Server Error", "CheckID \"service:service-A\" does not have associated TTL"));
    consulHeartbeatTask.run();
    ArgumentCaptor<NewService> serviceCaptor = ArgumentCaptor.forClass(NewService.class);
    ArgumentCaptor<String> tokenCaptor = ArgumentCaptor.forClass(String.class);
    verify(consulClient, atLeastOnce()).agentServiceRegister(serviceCaptor.capture(), tokenCaptor.capture());
    assertThat(serviceCaptor.getValue()).isSameAs(service);
}
Also used : ConsulHeartbeatTask(org.springframework.cloud.consul.discovery.TtlScheduler.ConsulHeartbeatTask) NewService(com.ecwid.consul.v1.agent.model.NewService) OperationException(com.ecwid.consul.v1.OperationException) Test(org.junit.Test)

Example 4 with OperationException

use of com.ecwid.consul.v1.OperationException in project spring-cloud-consul by spring-cloud.

the class ConsulHeartbeatTaskTests method notEligibleForReRegistration.

@Test
public void notEligibleForReRegistration() {
    TtlScheduler ttlScheduler = new TtlScheduler(heartbeatProperties, discoveryProperties, consulClient, ReregistrationPredicate.DEFAULT);
    ConsulHeartbeatTask consulHeartbeatTask = new ConsulHeartbeatTask(serviceId, ttlScheduler);
    heartbeatProperties.setReregisterServiceOnFailure(true);
    NewService service = new NewService();
    service.setId(serviceId);
    ttlScheduler.add(service);
    OperationException operationException = new OperationException(400, "Internal Server Error", "CheckID \"service:service-A\" does not have associated TTL");
    given(consulClient.agentCheckPass("service:" + serviceId)).willThrow(operationException);
    assertThatThrownBy(consulHeartbeatTask::run).isSameAs(operationException);
}
Also used : ConsulHeartbeatTask(org.springframework.cloud.consul.discovery.TtlScheduler.ConsulHeartbeatTask) NewService(com.ecwid.consul.v1.agent.model.NewService) OperationException(com.ecwid.consul.v1.OperationException) Test(org.junit.Test)

Example 5 with OperationException

use of com.ecwid.consul.v1.OperationException in project spring-cloud-consul by spring-cloud.

the class ConsulHeartbeatTaskTests method enableReRegistration.

@Test
public void enableReRegistration() {
    TtlScheduler ttlScheduler = new TtlScheduler(heartbeatProperties, discoveryProperties, consulClient, ReregistrationPredicate.DEFAULT);
    ConsulHeartbeatTask consulHeartbeatTask = new ConsulHeartbeatTask(serviceId, ttlScheduler);
    heartbeatProperties.setReregisterServiceOnFailure(true);
    NewService service = new NewService();
    service.setId(serviceId);
    ttlScheduler.add(service);
    given(consulClient.agentCheckPass("service:" + serviceId)).willThrow(new OperationException(500, "Internal Server Error", "CheckID \"service:service-A\" does not have associated TTL"));
    consulHeartbeatTask.run();
    ArgumentCaptor<NewService> serviceCaptor = ArgumentCaptor.forClass(NewService.class);
    ArgumentCaptor<String> tokenCaptor = ArgumentCaptor.forClass(String.class);
    verify(consulClient, atLeastOnce()).agentServiceRegister(serviceCaptor.capture(), tokenCaptor.capture());
    assertThat(serviceCaptor.getValue()).isSameAs(service);
}
Also used : ConsulHeartbeatTask(org.springframework.cloud.consul.discovery.TtlScheduler.ConsulHeartbeatTask) NewService(com.ecwid.consul.v1.agent.model.NewService) OperationException(com.ecwid.consul.v1.OperationException) Test(org.junit.Test)

Aggregations

OperationException (com.ecwid.consul.v1.OperationException)7 Test (org.junit.Test)6 NewService (com.ecwid.consul.v1.agent.model.NewService)4 ConsulHeartbeatTask (org.springframework.cloud.consul.discovery.TtlScheduler.ConsulHeartbeatTask)4 Event (com.ecwid.consul.v1.event.model.Event)1 Base64Utils.decodeFromString (org.springframework.util.Base64Utils.decodeFromString)1