I was using Flux.concatDelayError because I want to subscribe to multiple Monos one by one, and also want to know if something has failed.
However, now I would also like to short-circuit if one of my Monos completes with a specific type of error.
Is this possible easily?
Using onErrorResume operator, you could configure a conditional fallback to Mono.empty() for each Mono:
package com.example;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import static java.util.function.Predicate.not;
public class ReactorExample
{
public static void main(String[] args)
{
Mono<String> mono = Mono.just("first").doOnNext(a -> System.out.println(a + " was called."));
Mono<String> mono2 = Mono.<String>error(new RuntimeException("Not terminating error."))
.onErrorResume(not(ShortCircuitingException.class::isInstance), e -> Mono.empty());
Mono<String> mono3 = Mono.just("third").doOnNext(a -> System.out.println(a + " was called."));
Mono<String> mono4 = Mono.<String>error(new ShortCircuitingException())
.onErrorResume(not(ShortCircuitingException.class::isInstance), e -> Mono.empty());
Mono<String> mono5 = Mono.just("fifth").doOnNext(a -> System.out.println(a + " was called."));
Flux.concat(mono, mono2, mono3, mono4, mono5)
.collectList()
.block();
}
private static class ShortCircuitingException extends RuntimeException
{
}
}
Output:
first was called.
third was called.
Exception in thread "main" com.example.ReactorExample$ShortCircuitingException
Related
I am using reactor in a project, and one of the features calls a blocking service, which connects to a device and gets an infinite stream of events.
I am trying to do a load test to see how many calls can I make to the blocking service.
I am generating around 1000 requests to the blocking service
Flux.just("ip1", "ip2", "ip3", "ip4")
.repeat(250)
The problem is that reactor is only processing the first 256 requests, after that it isn't making any more requests.
When I added the .log("preConnect") I can see that it is logging only one request(256) from the downstream subscriber.
I don't understand what I am doing wrong.
I am attaching simplified example which can reproduce the issue.
package test.reactor;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;
import java.util.stream.Stream;
public class ReactorTest {
#Test
void testLoad() throws InterruptedException {
AtomicInteger id = new AtomicInteger(0);
Flux.just("ip1", "ip2", "ip3", "ip4")
.repeat(250) // will create a total of 1004 messages
.map(str -> str + " id: " + id.incrementAndGet())
.log("preConnect")
.flatMap(this::blocking)
.log()
.subscribeOn(Schedulers.parallel())
.subscribe();
new CountDownLatch(1).await();
}
private Flux<String> blocking(String ip) {
Mono<String> connectMono = Mono.fromCallable(this::connect)
.subscribeOn(Schedulers.boundedElastic())
.map(msg -> "Connected: "+ip + msg);
Flux<String> streamFlux = Mono.fromCallable(this::infiniteNetworkStream)
.subscribeOn(Schedulers.boundedElastic())
.flatMapMany(Flux::fromStream)
.map(msg -> ip + msg);
return connectMono.concatWith(streamFlux);
}
private Stream<String> infiniteNetworkStream() {
return Stream.generate(new Supplier<String>() {
#Override
public String get() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
return "Hello";
}
});
}
private String connect() throws Exception{
Thread.sleep(100);
return "success";
}
}
Figured out the issue, it has to do with flatmap, the default concurrency for flatmap is 256. It will not request more items from the upstream publisher until the current subscriptions go below 256.
In my case since my flux is infinite, it wasn't processing any after 256.
The solution I found was to increase the concurrency
Flux.just("ip1", "ip2", "ip3", "ip4")
.repeat(250) // will create a total of 1004 messages
.map(str -> str + " id: " + id.incrementAndGet())
.log("preConnect")
.flatMap(this::blocking, 1000) // added 1000 here to increase concurrency
.log()
.subscribeOn(Schedulers.parallel())
.subscribe();
i am not sure my understanding to errorHandler and returnExceptions is right or not.
but here is my goal: i sent a message from App_A, use #RabbitListener to receive message in App_B.
according to the doc
https://docs.spring.io/spring-amqp/docs/2.1.3.BUILD-SNAPSHOT/reference/html/_reference.html#annotation-error-handling
i assume if APP_B has a business exception during process the message,through set errorHandler and returnExceptions in a right way on #RabbitListener can let the exception back to App_A.
do I understood correctly?
if i am rigth, how to use it in a right way?
with my code, i get nothing in APP_A .
here is my code in APP_B
errorHandler:
#Component(value = "errorHandler")
public class ErrorHandler implements RabbitListenerErrorHandler {
#Override
public Object handleError(Message arg0, org.springframework.messaging.Message<?> arg1,
ListenerExecutionFailedException arg2) throws ListenerExecutionFailedException {
throw new ListenerExecutionFailedException("msg", arg2, null);
}
}
RabbitListener:
#RabbitListener(
bindings = #QueueBinding(
value = #Queue(value = "MRO.updateBaseInfo.queue", durable = "true"),
exchange = #Exchange(name = "MRO_Exchange", type = ExchangeTypes.DIRECT, durable = "true"),
key = "baseInfoUpdate"
),
// errorHandler = "errorHandler",
returnExceptions = "true"
)
public void receiveLocationChangeMessage(String message){
BaseUpdateMessage newBaseInfo = JSON.parseObject(message, BaseUpdateMessage.class);
dao.upDateBaseInfo(newBaseInfo);
}
and code in APP_A
#Component
public class MessageSender {
#Autowired
private RabbitTemplate rabbitTemplate;
public void editBaseInfo(BaseUpdateMessage message)throws Exception {
//and i am not sure set RemoteInvocationAwareMessageConverterAdapter in this way is right
rabbitTemplate.setMessageConverter(new RemoteInvocationAwareMessageConverterAdapter());
rabbitTemplate.convertAndSend("MRO_Exchange", "baseInfoUpdate", JSON.toJSONString(message));
}
}
i am very confuse with three points:
1)do i have to use errorHandler and returnExceptions at the same time? i thought errorHandler is something like a postprocessor that let me custom exception.if i don't need a custom exception can i just set returnExceptions with out errorHandler ?
2)should the method annotated with #RabbitListener return something or void is just fine?
3)in the sender side(my situation is APP_A), does have any specific config to catch the exception?
my workspace environment:
Spring boot 2.1.0
rabbitMQ server 3.7.8 on docker
1) No, you don't need en error handler, unless you want to enhance the exception.
2) If the method returns void; the sender will end up waiting for timeout for a reply that will never arrive, just in case an exception might be thrown; that is probably not a good use of resources. It's better to always send a reply, to free up the publisher side.
3) Just the RemoteInvocationAwareMessageConverterAdapter.
Here's an example:
#SpringBootApplication
public class So53846303Application {
public static void main(String[] args) {
SpringApplication.run(So53846303Application.class, args);
}
#RabbitListener(queues = "foo", returnExceptions = "true")
public String listen(String in) {
throw new RuntimeException("foo");
}
#Bean
public ApplicationRunner runner(RabbitTemplate template) {
template.setMessageConverter(new RemoteInvocationAwareMessageConverterAdapter());
return args -> {
try {
template.convertSendAndReceive("foo", "bar");
}
catch (Exception e) {
e.printStackTrace();
}
};
}
}
and
org.springframework.amqp.AmqpRemoteException: java.lang.RuntimeException: foo
at org.springframework.amqp.support.converter.RemoteInvocationAwareMessageConverterAdapter.fromMessage(RemoteInvocationAwareMessageConverterAdapter.java:74)
at org.springframework.amqp.rabbit.core.RabbitTemplate.convertSendAndReceive(RabbitTemplate.java:1500)
at org.springframework.amqp.rabbit.core.RabbitTemplate.convertSendAndReceive(RabbitTemplate.java:1433)
at org.springframework.amqp.rabbit.core.RabbitTemplate.convertSendAndReceive(RabbitTemplate.java:1425)
at com.example.So53846303Application.lambda$0(So53846303Application.java:28)
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:804)
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:794)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:324)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248)
at com.example.So53846303Application.main(So53846303Application.java:15)
Caused by: java.lang.RuntimeException: foo
at com.example.So53846303Application.listen(So53846303Application.java:20)
As you can see, there is a local org.springframework.amqp.AmqpRemoteException with the cause being the actual exception thrown on the remote server.
I'm trying to create a Map from a List using Streams.
The key should be the name of the original item,
The value should be some derived data.
After .map() the stream consists of Integers and at the time of .collect() I can't access "foo" from the previous lambda. How do I get the original item in .toMap()?
Can this be done with Streams or do I need .forEach()?
(The code below is only for demonstration, the real code is of course much more complex and I can't make doSomething() a method of Foo).
import java.util.ArrayList;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
public class StreamTest {
public class Foo {
public String getName() {
return "FOO";
}
public Integer getValue() {
return 42;
}
}
public Integer doSomething(Foo foo) {
return foo.getValue() + 23;
}
public Map<String, Integer> run() {
return new ArrayList<Foo>().stream().map(foo -> doSomething(foo)).collect(Collectors.toMap(foo.getName, Function.identity()));
}
public static void main(String[] args) {
StreamTest streamTest = new StreamTest();
streamTest.run();
}
}
It appears to me it’s not that complicated. Am I missing something?
return Stream.of(new Foo())
.collect(Collectors.toMap(Foo::getName, this::doSomething));
I’m rather much into method references. If you prefer the -> notation, use
return Stream.of(new Foo())
.collect(Collectors.toMap(foo -> foo.getName(), foo -> doSomething(foo)));
Either will break (throw an exception) if there’s more than one Foo with the same name in your stream.
I am new to atmosphere-websocket. I am working on private chat. I am using dropwizard framework.
Below is my code:
service.java:
#Override
public void run(ServiceConfiguration config, Environment environment)throws Exception {
AtmosphereServlet servlet = new AtmosphereServlet();
servlet.framework().addInitParameter("com.sun.jersey.config.property.packages", "dk.cooldev.chatroom.resources.websocket");
servlet.framework().addInitParameter(ApplicationConfig.WEBSOCKET_CONTENT_TYPE, "application/json");
servlet.framework().addInitParameter(ApplicationConfig.WEBSOCKET_SUPPORT, "true");
ServletRegistration.Dynamic servletHolder = environment.servlets().addServlet("Chat", servlet);
servletHolder.addMapping("/chat/*");``
}
ChatRoom.java:
package resource;
import org.atmosphere.config.service.DeliverTo;
import org.atmosphere.config.service.Disconnect;
import org.atmosphere.config.service.ManagedService;
import org.atmosphere.config.service.Message;
import org.atmosphere.config.service.PathParam;
import org.atmosphere.config.service.Ready;
import org.atmosphere.cpr.AtmosphereResource;
import org.atmosphere.cpr.AtmosphereResourceEvent;
import org.atmosphere.cpr.AtmosphereResourceFactory;
import org.atmosphere.cpr.Broadcaster;
import org.atmosphere.cpr.BroadcasterFactory;
import org.atmosphere.cpr.MetaBroadcaster;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import representation.UserMessage;
import utility.ChatProtocol;
import utility.JacksonEncoder;
import utility.ProtocolDecoder;
import utility.UserDecoder;
import javax.inject.Inject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.concurrent.ConcurrentHashMap;
#ManagedService(path = "/chat/{room: [a-zA-Z][a-zA-Z_0-9]*}")
public class ChatRoom {
private final Logger logger = LoggerFactory.getLogger(ChatRoom.class);
private final ConcurrentHashMap<String, String> users = new ConcurrentHashMap<String, String>();
private final static String CHAT = "/chat/";
#PathParam("room")
private String chatroomName;
#Inject
private BroadcasterFactory factory;
#Inject
private AtmosphereResourceFactory resourceFactory;
#Inject
private MetaBroadcaster metaBroadcaster;
/**
* Invoked when the connection as been fully established and suspended, e.g ready for receiving messages.
*
* #param r
*/
#Ready(encoders = {JacksonEncoder.class})
#DeliverTo(DeliverTo.DELIVER_TO.ALL)
public ChatProtocol onReady(final AtmosphereResource r) {
r.suspend();
logger.info("Browser {} connected.", r.uuid());
return new ChatProtocol(users.keySet(), getRooms(factory.lookupAll()));
}
private static Collection<String> getRooms(Collection<Broadcaster> broadcasters) {
Collection<String> result = new ArrayList<String>();
for (Broadcaster broadcaster : broadcasters) {
if (!("/*".equals(broadcaster.getID()))) {
// if no room is specified, use ''
String[] p = broadcaster.getID().split("/");
result.add(p.length > 2 ? p[2] : "");
}
};
return result;
}
/**
* Invoked when the client disconnect or when an unexpected closing of the underlying connection happens.
*
* #param event
*/
#Disconnect
public void onDisconnect(AtmosphereResourceEvent event) {
if (event.isCancelled()) {
// We didn't get notified, so we remove the user.
users.values().remove(event.getResource().uuid());
logger.info("Browser {} unexpectedly disconnected", event.getResource().uuid());
} else if (event.isClosedByClient()) {
logger.info("Browser {} closed the connection", event.getResource().uuid());
}
}
/**
* Simple annotated class that demonstrate how {#link org.atmosphere.config.managed.Encoder} and {#link org.atmosphere.config.managed.Decoder
* can be used.
*
* #param message an instance of {#link ChatProtocol }
* #return
* #throws IOException
*/
#Message(encoders = {JacksonEncoder.class}, decoders = {ProtocolDecoder.class})
public ChatProtocol onMessage(ChatProtocol message) throws IOException {
if (!users.containsKey(message.getAuthor())) {
users.put(message.getAuthor(), message.getUuid());
return new ChatProtocol(message.getAuthor(), " entered room " + chatroomName, users.keySet(), getRooms(factory.lookupAll()));
}
if (message.getMessage().contains("disconnecting")) {
users.remove(message.getAuthor());
return new ChatProtocol(message.getAuthor(), " disconnected from room " + chatroomName, users.keySet(), getRooms(factory.lookupAll()));
}
message.setUsers(users.keySet());
logger.info("{} just send {}", message.getAuthor(), message.getMessage());
return new ChatProtocol(message.getAuthor(), message.getMessage(), users.keySet(), getRooms(factory.lookupAll()));
}
#Message(decoders = {UserDecoder.class})
public void onPrivateMessage(UserMessage user) throws IOException {
String userUUID = users.get(user.getUser());
if (userUUID != null) {
// Retrieve the original AtmosphereResource
AtmosphereResource r = resourceFactory.find(userUUID);
if (r != null) {
ChatProtocol m = new ChatProtocol(user.getUser(), " sent you a private message: " + user.getMessage().split(":")[1], users.keySet(), getRooms(factory.lookupAll()));
if (!user.getUser().equalsIgnoreCase("all")) {
factory.lookup(CHAT + chatroomName).broadcast(m, r);
}
}
} else {
ChatProtocol m = new ChatProtocol(user.getUser(), " sent a message to all chatroom: " + user.getMessage().split(":")[1], users.keySet(), getRooms(factory.lookupAll()));
metaBroadcaster.broadcastTo("/*", m);
}
}
}
Now, when I run my application , its throwing
resource.ChatRoom: Browser 72196f81-3425-4137-8f37-c5aa6b134534 connected.
WARN: org.atmosphere.cpr.AtmosphereResourceImpl: Exception during suspend() operation java.lang.NullPointerException
Please help me out to resolve this exception.
If you are getting that warning then your socket should have been removed from the broadcast listeners list . Message that is delivered to the broadcaster which calls outgoingBroadcast(Object message) is null and null case isn't handled . In drop-wizard that message becomes null for onReady Method . Quick fix is to extend the broadcaster and override that method and handle the null case .
I hope it helps . I was using RedisBroadcaster and I had same issue and I fixed by handling the null case .
public void outgoingBroadcast(Object message) {
// Marshal the message outside of the sync block.
if(message == null){
return;
}
String contents = message.toString();
I was just setting up the chat samples my self and hit the same issue. In my case, an injected field was not available.
In your case, I would say it is one of
#Inject
private BroadcasterFactory factory;
#Inject
private AtmosphereResourceFactory resourceFactory;
#Inject
private MetaBroadcaster metaBroadcaster;
That are not wired in correctly. I have no used drop-wizard before so I can't advise on how to make sure these are configured correctly.
This type of issue was easy enough to see once I configured atmosphere for trace logging. e.g. for logback add the following line
<logger name="org.atmosphere" level="TRACE" />
Thanks
Ryan
i'm trying to use quartz to schedule jobs in grails with out using the plugin.
this is the code:
1 - RunMeTask.java
package tt;
public class RunMeTask {
public void printMe() {
System.out.println("Run Me ~");
}
}
2 - resources.groovy (under conf/spring)
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.scheduling.quartz.SimpleTriggerBean;
import tt.RunMeTask
beans = {
runMeTask(RunMeTask){}
runMeJob(JobDetailFactoryBean) {
targetObject = ref('runMeTask')
targetMethod = "printMe"
}
simpleTrigger(SimpleTriggerBean){
jobDetail = ref('runMeJob')
repeatInterval = "5000"
startpDelay = "1000"
}
schedulerFactoryBean(SchedulerFactoryBean){
jobDetails = [ref('runMeJob')]
triggers = [ref('simpleTrigger')]
}
}
i get the following exception:
Error Fatal error during compilation org.apache.tools.ant.BuildException: java.lang.IncompatibleClassChangeError: class org.springframework.scheduling.quartz.SimpleTriggerBean has interface org.quartz.SimpleTrigger as super class (Use --stacktrace to see the full trace)
can anyone help?
ok i figure it out. wasn't that hard when i think about it.the good thing about it its just as simple as you would do it in java and no plugin that may or may not work on grails certain version or any trouble that can caused by using a plugin.
there is 1 change in the code from the question:
RunMeTask.java (this can also be RunMeTask.groovy) must implement runnable and so
it look like this:
package tt;
import java.io.File;
import java.io.IOException;
import java.util.Random;
public class RunMeTask implements Runnable {
static Random r = new Random();
public void printMe() throws IOException {
File f = new File("c:\ofer.txt"+r.nextInt());
f.createNewFile();
System.out.println("fff");
}
public void run(){
try {
printMe();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
one funny thing is the println of "fff" occures only 2 times but a new file is created as expected every 5 seconds.
ok so thats it now a new file is created every 5 seconds in your c directory.
no plugin and no hassle. if anyone know why the System.out.println("fff"); occures only 2 times i will be happy to know.
thanks