Global Events Add-on

Hi everyone,

We have recently released a new add-on that enables propagating Spring’s application events to all application blocks of the distributed system. You can find it on our marketplace and on GitHub.

With this addon, you can just publish an event via the Events bean, say on the middleware, and all subscribers will receive it no matter where they are running - on the web frontend, on another middleware server (if you have a cluster), etc.

The add-on is not battle-tested yet, so please report about any problems here on the forum or create GitHub issues.

9 Likes

Hi @knstvk

Congrate finally cuba release this feature,

Unfortunately I’m trying this add on but no luck

I’m Using cuba platform 6.8.6

the log says Events is null, despite I’ve been @Inject Events class

Hi Arifian,

Thank you for trying out the add-on.
Where do you inject the Events bean? Could you show the code?
BTW, the Events bean is a part of the platform, not the add-on.

1. I put it in global module , and Event somehow become null in BeanPropagateEvents.java


---BeanPropagateEvents.java

import com.google.common.base.Strings;
import com.haulmont.cuba.core.global.Events;
import org.springframework.stereotype.Component;

import javax.inject.Inject;

@Component("patrolfoster_eventbean")
public class BeanPropagateEvents {
    @Inject
    private Events events;



    public String sendUiNotificationEvent(String message) {



        PropagateEvents propagateEvents =new PropagateEvents(this, Strings.isNullOrEmpty(message) ? "test" : message);

        events.publish(propagateEvents);
        return "done";
    }


      }

    --------PropagateEvents.java--------

    import com.haulmont.addon.globalevents.GlobalApplicationEvent;
    import com.haulmont.addon.globalevents.GlobalUiEvent;

     public class PropagateEvents extends GlobalApplicationEvent implements 
     GlobalUiEvent {
    /**
     * Create a new ApplicationEvent.
     *
     * @param source the object on which the event initially occurred (never {@code null})
     *
     */

private String message;
public PropagateEvents(Object source,String message) {
    super(source);

    this.message=message;
}


@Override
public Object getSource() {
    return super.getSource();
}

public String getMessage() {
    return message;
}

}

2. I intend to propagate event from REST to UI

-- REST part for call the bean
  new BeanPropagateEvents().sendUiNotificationEvent(userSessionSource.getUserSession().getUser().getLogin());

 ---UI part for receiver
 
 @EventListener
        public void onUiNotificationEvent(PropagateEvents event) {

         showMessageDialog("Event 
        Muncul",event.getMessage(),MessageType.CONFIRMATION);
   }

Don’t create beans with the constructor: new BeanPropagateEvents().
You have to inject them or obtain via AppBeans.get(). Then they become managed by the Spring container and injection works.

Also, please format your messages properly.

@knstvk : What format I should use for my messages ?

It’s Markdown as on Github, see for example here.

oh thanks for advice, sorry for my bad message format

Anyway after my event null problem solved,

My Eventlistener in web screen is not received anything

Could you provide the part of app.log content after publishing the event? It should contain log messages from the com.haulmont.addon.globalevents.* loggers that may help us to track down the issue.

Hi,

Thanks, already fixed,

Apparently something wrong with my deployment , after redeploy my project to tomcat
global-events-addon is work as expected :+1::+1:

Hi Everyone,

I created UiNotificationEvent in global module by extending GlobalApplicationEvent and implementing GlobalUiEvent.

When the middleware receive entitychangedevent i am sending the UiNotification Event. I dont see the event receiving the UI controller. Is there any additional settings or environment update i need to add for send/receive global events across layers.

Below is my code:

@EventListener
public void beforeCommit(EntityChangedEvent<Submission, Long> event) {
    Id<Submission, Long> entityId = event.getEntityId();
    EntityChangedEvent.Type changeType = event.getType();
    AttributeChanges changes = event.getChanges();
    System.out.println("before commit event"+changes.getAttributes().iterator().next().toString());
    String message = "Received commit changes from middle layer";
    //  globaleventservice.sendEvent(new UiNotificationEvent(this, Strings.isNullOrEmpty(message) ? "test" : message));
    events.publish(new UiNotificationEvent(this, Strings.isNullOrEmpty(message) ? "test" : message));
} 

UI Controller Listener –

@EventListener
public void onUiNotificationEvent(UiNotificationEvent event) {
    System.out.println("Received {}"+ event.getMessage());
    if(submissionDc.getItem() != null) {
        dataManager.reload(submissionDc.getItem(), "submission-view");
    }else{
        System.out.println("current item is null in uinotification event");
    }
}

Please help me in resolving the issue.
Thanks in advance for the help

Hi @paddu.bits

First I want to know how do you deploy your projects? in single WAR or double WAR? if in your case the client is deployed in the same JVM as middleware (single WAR) set cuba.useLocalServiceInvocation application property is set to true
otherwise set false

I have two war files(app.war, app-core.war) and i am using tomcat server from cuba studio.
I have cuba.useLocalServiceInvocation set to true.
Kindly let me know if i need to make any changes

hi, @paddu.bits sorry I editing my previous comment because of misleading information, so if you deploying your project in 2 war files, please set cuba.useLocalServiceInvocation to false

1 Like

plus check your catalina.out log files and notice if there is WebSocket log info just like my catalina.out screen capture below Screenshot%202019-05-23_12-46-39-258

Hi Arifian,

I have set cuba.useLocalServiceInvocation to false and tried i still see UI event not receiving controller.
I dont see any logs for websocket in catalina.out.
Do i need to change anything in gradle build file or any properties for events communication between layers

Thanks Everyone I was able to resolve issue.

1 Like