About JGroups

JGroups is a reliable group communication toolkit written entirely in Java. It is based on IP multicast (although TCP can also be used as transport): link.

How to use JGroups module

Import JGroups module into your project

Add the needed dependencies into your pom.xml

						
							org.ddbstoolkit.toolkit.modules.middleware.jgroups
							ddbstoolkit-jgroups
							1.0.0-beta2
							compile
						

Create your data model and your data module

You need to create your data model and instantiate your DistributableEntityManager of your needed module.

Start your JGroups receiver interface

				DistributableReceiverInterface receiver = null;
				try {
					receiver = new JGroupReceiver(manager, "defaultCluster", "receiver");
					receiver.start();
				} finally {
					if(receiver != null) {
						receiver.stop();
					}
				}

Your JGroups program will now received data actions from the cluster "defaultCluster" and his peer name is "receiver".

Instantiate your JGroups sender interface

				DistributableSenderInterface senderInterface = new JGroupSender("defaultCluster", "sender");

The sender interface will send data command actions to receiver interfaces in the cluster "defaultCluster". In the cluster, the peer will be named "sender".

JGroups Module actions

Get list of receiver peers

				senderInterface.open();
		
				List peers = senderInterface.getListPeers();
				
				senderInterface.close();

Add an element

				senderInterface.open();
		
				Actor anActor = new Actor();
				anActor.setPeerUid("receiver");
				anActor.setActorName("Steven");
					
				senderInterface.add(anActor);
				
				senderInterface.close();

Update an element

				senderInterface.open();
		
				Actor anActor = new Actor();
				anActor.setPeerUid("receiver");
				anActor.setActorId(3);
				anActor.setActorName("Steven");
					
				senderInterface.update(anActor);
				
				senderInterface.close();

Delete an element

				senderInterface.open();
		
				Actor anActor = new Actor();
				anActor.setPeerUid("receiver");
				anActor.setActorId(3);
					
				senderInterface.delete(anActor);
				
				senderInterface.close();

Read an element

				senderInterface.open();
		
				Actor anActor = new Actor();
				anActor.setPeerUid("receiver");
				anActor.setActorId(3);
					
				anActor = senderInterface.read(anActor);
				
				senderInterface.close();

Read last added element

				senderInterface.open();
		
				Actor anActor = new Actor();
				anActor.setPeerUid("receiver");
		
				Actor anActor = senderInterface.readLastElement(anActor);
				
				senderInterface.close();

List elements

				senderInterface.open();
		
				//List all films
				List films = senderInterface.listAll(new Film(), null, null);
		
				//List all films ordered by film_ID
				films = senderInterface.listAll(new Film(), null, OrderBy.get("filmId", OrderByType.ASC));
				
				List conditions = new ArrayList<>();
        		conditions.add("film_name LIKE 'S%'");
				
				//List all films who starts with 'S'
				films = senderInterface.listAll(new Film(), conditions, null);
				
				//Same thing with Conditions
				Conditions conditionLike = Conditions.createConditions().add(
				Conditions.like("filmName", "S%"));
				List results = senderInterface.listAll(new Film(),conditionLike, null);
				
				senderInterface.close();

Load elements linked to an entity

				senderInterface.open();
 
				Film lastFilmAdded = senderInterface.readLastElement(new Actor());
				
				//List actors of a movie ordered by actor_id field
				lastFilmAdded = senderInterface.loadArray(lastFilmAdded, "actors", OrderBy.get("actorId", OrderByType.ASC));				
 
				senderInterface.close();

Execute transactions

				senderInterface.open();
				
				//Start a new transaction
				DDBSTransaction transactionAddCommit = new DDBSTransaction("myTransaction");
     
				Actor anActor = new Actor();
				anActor.setActorName("Steven");
				
				transactionAddCommit.add(filmToAdd);
		
				//Execute the transaction
				senderInterface.executeTransaction(transactionAddCommit);
				
				//Commit the transaction
				senderInterface.commit(transactionAddCommit);
				
				//Rollback the transaction
				senderInterface.rollback(transactionAddCommit);				
 
				senderInterface.close();