Skip to content

Commit

Permalink
Use a lambda in MainView to match the official example, move try/catc…
Browse files Browse the repository at this point in the history
…h into lambda and use info-level logging in AbstractBroadcaster
  • Loading branch information
mrts committed Jan 19, 2024
1 parent a74ba08 commit b38d605
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
18 changes: 10 additions & 8 deletions src/main/java/org/test/AbstractBroadcaster.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ abstract class AbstractBroadcaster<M> {
private Event<M> messageEvent;

public Registration register(Consumer<M> listener) {
log.debug("{} registering {}", this, listener);
log.info("{} registering {}", this, listener);
if (!listeners.contains(listener)) {
listeners.add(listener);
return () -> unregister(listener);
Expand All @@ -50,18 +50,20 @@ private void onMessageAsync(@ObservesAsync M message) {
}

private void onMessage(@Observes(during = TransactionPhase.AFTER_SUCCESS) M message) {
log.debug("{} got message {}", this, message);
log.info("{} got message {}, listeners are: {}", this, message, listeners);
for (final Consumer<M> listener : listeners) {
try {
executorService.execute(() -> listener.accept(message));
} catch (Exception e) {
log.error("Error processing message in listener: {}", listener, e);
}
executorService.execute(() -> {
try {
listener.accept(message);
} catch (Exception e) {
log.error("Error processing message in listener: {}", listener, e);
}
});
}
}

private void unregister(Consumer<M> listener) {
log.debug("{} unregistering {}", this, listener);
log.info("{} unregistering {}", this, listener);
if (listeners.contains(listener)) {
listeners.remove(listener);
} else {
Expand Down
23 changes: 15 additions & 8 deletions src/main/java/org/test/MainView.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
package org.test;

import com.vaadin.cdi.annotation.CdiComponent;
import com.vaadin.flow.component.AttachEvent;
import com.vaadin.flow.component.DetachEvent;
import com.vaadin.flow.component.Key;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.button.ButtonVariant;
import com.vaadin.flow.component.notification.Notification;
Expand All @@ -19,11 +21,12 @@

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import java.util.function.Consumer;
import java.util.Objects;

@Route("")
@CdiComponent
public class MainView extends VerticalLayout implements Consumer<Message> {
@Slf4j
public class MainView extends VerticalLayout {

@Inject
private Broadcaster broadcaster;
Expand All @@ -44,21 +47,25 @@ public void init() {
broadcastButton.addClickShortcut(Key.ENTER);

add(textField, broadcastButton, asyncBroadcastButton);
}

broadcasterRegistration = broadcaster.register(this);
@Override
protected void onAttach(AttachEvent attachEvent) {
final UI ui = attachEvent.getUI();
Objects.requireNonNull(ui, "UI cannot be null");
broadcasterRegistration = broadcaster.register(message -> {
log.info("{} received message: {}", this, message.getText());
ui.access(() -> Notification.show(message.getText()));
}
);
}

@Override
protected void onDetach(DetachEvent detachEvent) {
super.onDetach(detachEvent);
if (broadcasterRegistration != null) {
broadcasterRegistration.remove();
broadcasterRegistration = null;
}
}

@Override
public void accept(Message message) {
getUI().ifPresent(ui -> ui.access(() -> Notification.show(message.getText())));
}
}

0 comments on commit b38d605

Please sign in to comment.