Series nineteen JBoss: JGroups building blocks using a return message to the group communication RspFilter filter

Content Overview

This section describes the interface JGroups building blocks RspFilter, particularly to provide a simple example to illustrate how the return message RspFilter JGroups building blocks of group communication filtered.

Example Description

We know based on building blocks of channels, the channel is a higher level of abstraction of the API, and are the building blocks of RspFilter primary interface, it can be more flexible model of transporting custom groups of filter response message. DETAILED RspFilter may determine whether to add the group to return a list of all nodes, determining when or after receiving a response message for a particular member, whether to terminate the group transporting transporting. As an example we return -3RpcDispatcher communication mode defined RequestOptions mode (GET_FIRST, GET_ALL, GET_MAJORITY, GET_NONE), but the response returned RspFilter returns the filtered overrides the RequestOptions defined.

This example is based  JBoss eighteen series: JGroups building blocks used to build a group communication application RpcDispatcher added RspFilter filter response is returned on its basis, the priority rules between the filter and the test mode in response GET_ALL RspFilter defined, by way of example and experience RspFilter making group communication more flexible, customizable.

Examples of steps

This example has three members in the cluster node1, node2 and node3, node1 is the coordinator (the first to join the cluster) is responsible for cluster view updates. RpcMethods class defines the getNodeName () method, the three start nodes are transporting the getNodeName () method on all nodes in the cluster RpcMethods class, and the process returns to wait. RspFilter response rule definition is not added node2 response returns a list (when the node is transporting method, the getNodeName ignored () method returns). Use JBoss Cluster Framework Demo described method shown, any download from SourceForge or compiled DEMO_HOME , startup script rspFilter.sh present example located in DEMO_HOME / bin. Next we turn to start three nodes:

./ rspFilter.sh -n node1

./ rspFilter.sh -n node2

./ rspFilter.sh -n node3

Note, -n specify a node name, Windows operating system using the corresponding .bat script.

Result analysis

1. node1, node2, and after node3 any one node starts we can see the log transporting members of the group getNodeName () method, as follows logs on node1 output:

09:48:07,614 INFO  [RspFilterTest] Call all members getNodeName()

2. node1, node2, and node3 any one group member node starts transporting the getNodeName () method returns the output can see the remote method, the remote method as printout on node3 Returns:

Responses:
  node1
  node3

As the name, node2 node is not returned, because the response rule defined RspFilter node2 is not added in response returns a list.

3. node1, node2 node3 in any of the log and run a node, we can see the log MyMembershipListener output when the group membership change is the group view is printed output, the following information for the log output on node1:

09:48:35,928 INFO  [MyMembershipListener] ViewAccepted, [node1|2] [node1, node2, node3]

[Node1 | 2] [node1, node2, node3] for the group view the printout, node1 | 2 represents the group coordinator for node1 and the view is updated three times (node1, node2, node3 then added to the cluster).

Code Analysis

All source code in this example may be in cluster / jgroups / stu / src / main / java /.../ blocks to find the next, then we to explain the above analysis results from the level of the code, which helps more intuitive understanding Building blocks RspFilter appreciated that interface.

30                 channel = new JChannel();
 31                 if(null != name) {
 32                         channel.setName(name);
 33                 }
 35                 messageListener = new MyMessageListener();
 36                 membershipListener = new MyMembershipListener();
 37                 rpcMethods = new RpcMethods(channel);
 38                 disp = new RpcDispatcher(channel, messageListener, membershipListener, rpcMethods);
 39                 channel.connect("RspFilterTestGroup");

As a jGroups 30-33 initialized channel, and the channel set name; lines 35-38 initializes MyMessageListener, MyMembershipListener, RpcMethods, then initialize RpcDispatcher; 39 channel line connected to a cluster (RspFilterTestGroup), channel is available only in a connected state mutually transmitting and receiving messages.

42                 String param = channel.getName();
 43                 MethodCall call = new MethodCall("getNodeName", new Object[]{param}, new Class[]{String.class});
 44                 logger.info("Call all members getNodeName()");
 45                 RequestOptions requestOptions = new RequestOptions(ResponseMode.GET_ALL, 0, false, new MyRspFilter());

As the parameter code segment () methods of transporting the cluster, called the MethodCall getNodeName instance of the specified remote method by a RpcDispatcher callRemoteMethods, passing the name of the current node, ResponseMode.GET_ALL means that we use GET_ALL mode, i.e., the method returns to wait for the node, to achieve RspFilter MyRspFilter interface, callRemoteMethods parameter is null, the method indicates that all nodes are transported;

48                 System.out.println("Responses:");
 49                 List<String> list = rsp_list.getResults();
 50                 for(Object obj : list) {
 51                         System.out.println("  " + obj);
 52                 }

As the getNodeName code segment are sequentially output print () method returns the result.

6 public class MyRspFilter implements RspFilter {
  7 
  8         public boolean isAcceptable(Object response, Address sender) {
  9                 String name = (String) response;
 10                 if(name.equals("node2")) {
 11                         return false;
 12                 }else {
 13                         return true;
 14                 }
 15         }

Line 6 MyRspFilter RspFilter implements the interface, rows 8-14 isAcceptable () method returns the message filtering rules, i.e. when the node name of the getNodeName () is obtained node2, the group communication ignore this message.


Reproduced in: https: //my.oschina.net/iwuyang/blog/197223

Guess you like

Origin blog.csdn.net/weixin_34301132/article/details/91897284