- Я хочу пробежать верблюжий маршрут, прохождение которого займет много времени. Я хочу
запустить этот маршрут с помощью запроса put. - Я хочу разрешить только 1 одновременное
выполнение для этого маршрута.
< li>Я хочу отменить любой другой запрос,
поступивший во время обработки маршрута. Я не хочу ставить их в очередь, чтобы
обработать позже, просто выбросьте их.
Код: Выделить всё
@Component
public class TestRoute2 extends RouteBuilder {
@Autowired
private MyService myService;
@Override
public void configure() {
AtomicBoolean isProcessing = new AtomicBoolean(false);
from("platform-http:/api/myservice?httpMethodRestrict=PUT")
.routeId("myServiceRoute")
.doTry()
.process(exchange -> {
// Cast the endpoint to SedaEndpoint to access the queue
SedaEndpoint sedaEndpoint = (SedaEndpoint)
getContext().getEndpoint("seda:handlePut");
int currentQueueSize = sedaEndpoint
.getQueue()
.size();
if (currentQueueSize > 0 || isProcessing.get()) {
throw new org.apache.camel.CamelExchangeException("Service busy", exchange);
}
// Mark service as busy
isProcessing.set(true);
})
.wireTap("seda:handlePut")
.setHeader("CamelHttpResponseCode", constant(204))
.endDoTry()
.doCatch(org.apache.camel.CamelExchangeException.class)
.setHeader("CamelHttpResponseCode", constant(503))
.setBody(constant("Service busy, please try again later"))
.end();
from("seda:handlePut?concurrentConsumers=1")
.doTry()
.bean(myService, "slowMethod") // runs for long time
.endDoTry()
.doFinally()
.process(exchange -> isProcessing.set(false));
}
}
Код: Выделить всё
@Override
public void configure() {
from("platform-http:/api/myservice?httpMethodRestrict=PUT")
.routeId("myServiceRoute")
.doTry()
.process(exchange -> {
SedaEndpoint sedaEndpoint = (SedaEndpoint)
getContext().getEndpoint("seda:handlePut");
int currentQueueSize = sedaEndpoint
.getQueue()
.size();
if (currentQueueSize > 0) {
throw new org.apache.camel.CamelExchangeException("Service busy", exchange);
}
})
.wireTap("seda:handlePut")
.setHeader("CamelHttpResponseCode", constant(204))
.endDoTry()
.doCatch(org.apache.camel.CamelExchangeException.class)
.setHeader("CamelHttpResponseCode", constant(503))
.setBody(constant("Service busy, please try again later"))
.end();
from("seda:handlePut?concurrentConsumers=1")
.bean(myService, "slowMethod");
}
Подробнее здесь: https://stackoverflow.com/questions/790 ... on-and-dis