Merge branch 'eugenp:master' into master
This commit is contained in:
commit
bc36bb2992
|
@ -0,0 +1,62 @@
|
|||
package com.baeldung.rxjava;
|
||||
import io.reactivex.Observable;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class RxJavaRetryWithDelayUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenObservable_whenSuccess_thenOnNext(){
|
||||
Observable.just(remoteCallSuccess())
|
||||
.subscribe(success -> {
|
||||
System.out.println("Success");
|
||||
System.out.println(success);
|
||||
}, err -> {
|
||||
System.out.println("Error");
|
||||
System.out.println(err);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenObservable_whenError_thenOnError(){
|
||||
Observable.just(remoteCallError())
|
||||
.subscribe(success -> {
|
||||
System.out.println("Success");
|
||||
System.out.println(success);
|
||||
}, err -> {
|
||||
System.out.println("Error");
|
||||
System.out.println(err);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenError_whenRetry_thenCanDelay(){
|
||||
Observable.just(remoteCallError())
|
||||
.retryWhen(attempts -> {
|
||||
return attempts.flatMap(err -> {
|
||||
if (customChecker(err)) {
|
||||
return Observable.timer(5000, TimeUnit.MILLISECONDS);
|
||||
} else {
|
||||
return Observable.error(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
private String remoteCallSuccess(){
|
||||
return "Success";
|
||||
}
|
||||
|
||||
private String remoteCallError(){
|
||||
// consider a network call that failed over here.
|
||||
return "Error";
|
||||
}
|
||||
|
||||
private boolean customChecker(Throwable t){
|
||||
// this will include custom logic that decides whether resubscription should occur or not
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.camel.exception;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class ExceptionHandlingSpringApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ExceptionHandlingSpringApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package com.baeldung.camel.exception;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.apache.camel.builder.RouteBuilder;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class ExceptionHandlingWithDoTryRoute extends RouteBuilder {
|
||||
|
||||
@Override
|
||||
public void configure() throws Exception {
|
||||
|
||||
from("direct:start-handling-exception")
|
||||
.routeId("exception-handling-route")
|
||||
.doTry()
|
||||
.process(new IllegalArgumentExceptionThrowingProcessor())
|
||||
.to("mock:received")
|
||||
.doCatch(IOException.class, IllegalArgumentException.class)
|
||||
.to("mock:caught")
|
||||
.doFinally()
|
||||
.to("mock:finally")
|
||||
.end();
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package com.baeldung.camel.exception;
|
||||
|
||||
import org.apache.camel.builder.RouteBuilder;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class ExceptionHandlingWithExceptionClauseRoute extends RouteBuilder {
|
||||
|
||||
@Autowired
|
||||
private ExceptionLoggingProcessor exceptionLogger;
|
||||
|
||||
@Override
|
||||
public void configure() throws Exception {
|
||||
onException(IllegalArgumentException.class).process(exceptionLogger)
|
||||
.handled(true)
|
||||
.to("mock:handled");
|
||||
|
||||
from("direct:start-exception-clause")
|
||||
.routeId("exception-clause-route")
|
||||
.process(new IllegalArgumentExceptionThrowingProcessor())
|
||||
.to("mock:received");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package com.baeldung.camel.exception;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.camel.Exchange;
|
||||
import org.apache.camel.Processor;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class ExceptionLoggingProcessor implements Processor {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(ExceptionLoggingProcessor.class);
|
||||
|
||||
@Override
|
||||
public void process(Exchange exchange) throws Exception {
|
||||
Map<String, Object> headersMap = exchange.getIn().getHeaders();
|
||||
|
||||
if (!headersMap.isEmpty()) {
|
||||
headersMap.entrySet()
|
||||
.stream()
|
||||
.forEach(e -> LOGGER.info("Header key [{}] -||- Header value [{}]", e.getKey(), e.getValue()));
|
||||
} else {
|
||||
LOGGER.info("Empty header");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package com.baeldung.camel.exception;
|
||||
|
||||
import org.apache.camel.Exchange;
|
||||
import org.apache.camel.Processor;
|
||||
import org.apache.camel.builder.RouteBuilder;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class ExceptionThrowingRoute extends RouteBuilder {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(ExceptionThrowingRoute.class);
|
||||
|
||||
@Override
|
||||
public void configure() throws Exception {
|
||||
|
||||
from("direct:start-exception")
|
||||
.routeId("exception-throwing-route")
|
||||
.process(new Processor() {
|
||||
|
||||
@Override
|
||||
public void process(Exchange exchange) throws Exception {
|
||||
LOGGER.error("Exception Thrown");
|
||||
throw new IllegalArgumentException("An exception happened on purpose");
|
||||
|
||||
}
|
||||
}).to("mock:received");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package com.baeldung.camel.exception;
|
||||
|
||||
import org.apache.camel.Exchange;
|
||||
import org.apache.camel.Processor;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class IllegalArgumentExceptionThrowingProcessor implements Processor {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(ExceptionLoggingProcessor.class);
|
||||
|
||||
@Override
|
||||
public void process(Exchange exchange) throws Exception {
|
||||
LOGGER.error("Exception Thrown");
|
||||
throw new IllegalArgumentException("An exception happened on purpose");
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package com.baeldung.camel.exception;
|
||||
|
||||
import org.apache.camel.EndpointInject;
|
||||
import org.apache.camel.ProducerTemplate;
|
||||
import org.apache.camel.component.mock.MockEndpoint;
|
||||
import org.apache.camel.test.spring.junit5.CamelSpringBootTest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
@CamelSpringBootTest
|
||||
class ExceptionHandlingWithDoTryRouteUnitTest {
|
||||
|
||||
@Autowired
|
||||
private ProducerTemplate template;
|
||||
|
||||
@EndpointInject("mock:caught")
|
||||
private MockEndpoint mock;
|
||||
|
||||
@Test
|
||||
void whenSendHeaders_thenExceptionRaisedAndHandledSuccessfully() throws Exception {
|
||||
mock.expectedMessageCount(1);
|
||||
|
||||
template.sendBodyAndHeader("direct:start-handling-exception", null, "fruit", "Banana");
|
||||
|
||||
mock.assertIsSatisfied();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package com.baeldung.camel.exception;
|
||||
|
||||
import org.apache.camel.EndpointInject;
|
||||
import org.apache.camel.ProducerTemplate;
|
||||
import org.apache.camel.component.mock.MockEndpoint;
|
||||
import org.apache.camel.test.spring.junit5.CamelSpringBootTest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
@CamelSpringBootTest
|
||||
class ExceptionHandlingWithExceptionClauseRouteUnitTest {
|
||||
|
||||
@Autowired
|
||||
private ProducerTemplate template;
|
||||
|
||||
@EndpointInject("mock:handled")
|
||||
private MockEndpoint mock;
|
||||
|
||||
@Test
|
||||
void whenSendHeaders_thenExceptionRaisedAndHandledSuccessfully() throws Exception {
|
||||
mock.expectedMessageCount(1);
|
||||
|
||||
template.sendBodyAndHeader("direct:start-exception-clause", null, "fruit", "Banana");
|
||||
|
||||
mock.assertIsSatisfied();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
package com.baeldung.camel.exception;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import org.apache.camel.CamelContext;
|
||||
import org.apache.camel.Exchange;
|
||||
import org.apache.camel.ExchangePattern;
|
||||
import org.apache.camel.ProducerTemplate;
|
||||
import org.apache.camel.test.spring.junit5.CamelSpringBootTest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
@CamelSpringBootTest
|
||||
class ExceptionThrowingRouteUnitTest {
|
||||
|
||||
@Autowired
|
||||
private ProducerTemplate template;
|
||||
|
||||
@Test
|
||||
void whenSendBody_thenExceptionRaisedSuccessfully() {
|
||||
CamelContext context = template.getCamelContext();
|
||||
Exchange exchange = context.getEndpoint("direct:start-exception")
|
||||
.createExchange(ExchangePattern.InOut);
|
||||
|
||||
exchange.getIn().setBody("Hello Baeldung");
|
||||
Exchange out = template.send("direct:start-exception", exchange);
|
||||
|
||||
assertTrue(out.isFailed(), "Should be failed");
|
||||
assertTrue(out.getException() instanceof IllegalArgumentException, "Should be IllegalArgumentException");
|
||||
assertEquals("An exception happened on purpose", out.getException().getMessage());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung.camel.exception;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class IllegalArgumentExceptionThrowingProcessorUnitTest {
|
||||
|
||||
@Test
|
||||
void whenProcessed_thenIllegalArgumentExceptionRaisedSuccessfully() {
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
new IllegalArgumentExceptionThrowingProcessor().process(null);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue