Send messages from service to gui

Hi

Is there any way send notification from service to web layer. In web layer I’d like
show notification witch was send from service. Can I use events mechanism for it ?

Hi,

The global events addon allows you to do that: Global Events – CUBA Platform

Another approach is done in the user-inbox addon, which is based on polling: User Inbox – CUBA Platform

I hope this helps…
Bye
Mario

2 Likes

Hi Mario

I try implement global events, code in java is below. I publish event, but I don’t know How I can receive event in web layer and
show notification.

@Service(DocumentNumerationService.NAME)
public class DocumentNumerationServiceBean implements DocumentNumerationService {
	@Inject
	private Persistence persistence;
	private int NUMBER_LEN = 40;

	@Inject
	private Events events;
	@Inject
	private UserSessionSource userSessionSource;

	@Transactional
	@Override
	public String getNextNumber(String numberMaskCode, Date date, String documentType) {

		String result = null;
		try (Transaction tx = persistence.createTransaction()) {
			EntityManager entityManager = persistence.getEntityManager();
			NumeratorManager nm = new NumeratorManager(entityManager, NUMBER_LEN, null, null);
			result = nm.getNextNumber(numberMaskCode, date, documentType, null);
			tx.commit();
			UserSession userSession = userSessionSource.getUserSession();
			events.publish(new MyUiNotificationEvent(userSession.getUser(), "message Hello"));
		}

		return result;
	}

Hi,

Take a look at the demo project GitHub - cuba-platform/global-events-demo: Global Events add-on demo project

You can receive global events in screen controllers:

public class Screen1 extends AbstractWindow {

    private static final Logger log = LoggerFactory.getLogger(Screen1.class);

    @Inject
    private Label receivedLab;

    private AtomicInteger count = new AtomicInteger();

    @EventListener
    public void onUiNotificationEvent(UiNotificationEvent event) {
        log.info("Received {}", event);
        receivedLab.setValue(count.incrementAndGet());
    }
}

For instance, you could extend the main window and add event handler there.