-
-
Notifications
You must be signed in to change notification settings - Fork 751
Multi tenancy pattern using JGroups
Jamal edited this page Dec 13, 2018
·
1 revision
Each tenant requires a separate JGroups cluster to receive messages. This can cause issues with too many sockets opened and port conflict issues if using TCP.
The JGroupsBroadcaster can act as a hub for many local broadcasters on each node. Each instance of a local broadcaster represents a tenant on that node. Only a single JGroups cluster is created and shared. Messages are relayed from the JGroupBroadcaster to the local ones.
The tenant name needs to get passed around in order to match the local broadcaster held in a map
Client connection: ` public void doGet(AtmosphereResource ar) {
String tenantName = ar.getRequest().getParameter("tenant");
String broadcasterName = "LocalBroadcaster-" + tenantName;
BroadcasterFactory factory = ar.getAtmosphereConfig().getBroadcasterFactory();
Broadcaster globalBroadcaster = factory.lookup(JGroupsBroadcasterTCP.class, "GlobalBroadcaster", true);
// Create local broadcaster
Broadcaster localBroadcaster = factory.lookup(DefaultBroadcaster.class, broadcasterName, true);
// Add local broadcaster to registry
localBroadcasterRegistry.putIfAbsent(tenantName, broadcasterName);
// Attach broadcaster to the client connection
ar.setBroadcaster(localBroadcaster);
// Create listener and add to global broadcaster
BroadcasterListener globalBcListener = new BroadcasterListener() {
@Override
public void onRemoveAtmosphereResource(Broadcaster b, AtmosphereResource r) {
}
@Override
public void onPreDestroy(Broadcaster b) {
}
@Override
public void onPostCreate(Broadcaster b) {
}
@Override
public void onMessage(Broadcaster b, Deliver deliver) {
String key = ((MyPayload) deliver.getMessage()).getTenantName();
String localBcName = localBroadcasterRegistry.get(key);
Broadcaster localBc = factory.lookup(DefaultBroadcaster.class, localBcName, true);
localBc.broadcast(deliver.getMessage());
}
@Override
public void onComplete(Broadcaster b) {
}
@Override
public void onAddAtmosphereResource(Broadcaster b, AtmosphereResource r) {
}
};
globalBroadcaster.addBroadcasterListener(globalBcListener);
ar.suspend();
}
`
Send events:
`Broadcaster br = broadcasterFactory.lookup(JGroupsBroadcasterTCP.class, "GlobalBroadcaster", true);
br.broadcast(context);`
- Understanding Atmosphere
- Understanding @ManagedService
- Using javax.inject.Inject and javax.inject.PostConstruct annotation
- Understanding Atmosphere's Annotation
- Understanding AtmosphereResource
- Understanding AtmosphereHandler
- Understanding WebSocketHandler
- Understanding Broadcaster
- Understanding BroadcasterCache
- Understanding Meteor
- Understanding BroadcastFilter
- Understanding Atmosphere's Events Listeners
- Understanding AtmosphereInterceptor
- Configuring Atmosphere for Performance
- Understanding JavaScript functions
- Understanding AtmosphereResourceSession
- Improving Performance by using the PoolableBroadcasterFactory
- Using Atmosphere Jersey API
- Using Meteor API
- Using AtmosphereHandler API
- Using Socket.IO
- Using GWT
- Writing HTML5 Server-Sent Events
- Using STOMP protocol
- Streaming WebSocket messages
- Configuring Atmosphere's Classes Creation and Injection
- Using AtmosphereInterceptor to customize Atmosphere Framework
- Writing WebSocket sub protocol
- Configuring Atmosphere for the Cloud
- Injecting Atmosphere's Components in Jersey
- Sharing connection between Browser's windows and tabs
- Understanding AtmosphereResourceSession
- Manage installed services
- Server Side: javadoc API
- Server Side: atmosphere.xml and web.xml configuration
- Client Side: atmosphere.js API