Skip to content

Message broker library used for event-driven microservice architecture.

Notifications You must be signed in to change notification settings

hughnguy/message-broker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

saas-message-broker

Build Status

Message broker used for event-driven architecture. Facilitates collaboration between our services.


Example usage

1) Create an Event

public class AssetShared extends Event {
    
    private String assetName;

    public AssetShared(String assetName) {
        this.assetName = assetName;
    }
    
    @Override
    public String getName() {
        return "ASSET_SHARED";
    }

    @Override
    public int getVersion() {
        return 1;
    }
    
    @Override
    public Map<String, Object> getPayload() {
        Map<String, Object> payload = new HashMap<>();
        payload.put("assetName", assetName);
        return payload;
    }
    
    @Override
    public void createFromPayload(Map<String, Object> payload) {
        assetName = (String) payload.get("assetName");
    }
}

2) Create an EventMessageConsumer. Add the Event class to the EventProcessMapper passed into the consumer's constructor

public class SendEmailNotification extends EventMessageConsumer {
    
    public SendEmailNotification() {
        super(
            new EventProcessMapper(AssetShared.class, null)
        );
    }
}

3) Implement an EventProcessor in order to process that event type (AssetShared)

public class AssetSharedEmailNotificationService implements EventProcessor<AssetShared> {

        public void processEvent(AssetShared assetShared) {
                /**
                 * Receive asset shared event and send an email here 
                 */
        }
}

4) Add the EventProcessor as the second parameter to the EventProcessMapper. That's it!

public class SendEmailNotification extends EventMessageConsumer {
    
    @Autowired
    public SendEmailNotification(AssetSharedEmailNotificationService assetSharedEmailNotificationService) {
        super(
            new EventProcessMapper(AssetShared.class, assetSharedEmailNotificationService)
        );
    }
}

🎉 The EventProcessor.processEvent() method will now be called every time the AssetShared event is published.


Feel free to add more events and consumers to perform multiple actions per event!

public class SendEmailNotification extends EventMessageConsumer {
    
    @Autowired
    public SendEmailNotification(
            AssetSharedEmailNotificationService assetSharedEmailNotificationService,
            CompanyCreatedEmailNotificationService companyCreatedEmailNotificationService,
            UserDeletedEmailNotificationService userDeletedEmailNotificationService
    ) {
        super(
            new EventProcessMapper(AssetShared.class, assetSharedEmailNotificationService),
            new EventProcessMapper(CompanyCreated.class, companyCreatedEmailNotificationService),
            new EventProcessMapper(UserDeleted.class, userDeletedEmailNotificationService)
        );
    }
}

public class SendTextNotification extends EventMessageConsumer {
    
    @Autowired
    public SendEmailNotification(AssetSharedTextNotificationService assetSharedTextNotificationService) {
        super(
            new EventProcessMapper(AssetShared.class, assetSharedTextNotificationService)
        );
    }
}

public class SendPushNotification extends EventMessageConsumer {
    
    @Autowired
    public SendPushNotification(AssetSharedPushNotificationService assetSharedPushNotificationService) {
        super(
            new EventProcessMapper(AssetShared.class, assetSharedPushNotificationService)
        );
    }
}

To publish an event, simply create an instance of the event and call publishEvent().


UserCreated event = new UserCreated();

MessageBroker.getInstance().publishEvent(event);

🎉 All microservices listening to that event will now be notified

About

Message broker library used for event-driven microservice architecture.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages