Using the Relation Service

In the previous section, we looked at each of the relation service classes that are necessary to create internal and external relations. In this section, we will look at source code examples for creating internal relations, as these are the easiest type to create. All of the source code examples in this section are taken from the relation package of the sample application that we have used throughout this book.

Before we can create a relation, we have to create the MBean server and an instance of the relation service MBean, and then register the relation service MBean with the MBean server:

try {
  MBeanServer server = MBeanServerFactory.createMBeanServer(  );
  boolean purgeImmediate = true;
  RelationService rs = new RelationService(purgeImmediate);
  ObjectName rsObjName = new ObjectName("AgentServices:name=Relation");
  server.registerMBean(rs, rsObjName);
  // . . .
} catch (Exception e) {
  // . . .
}

Next, we describe the roles in the relation using one or more RoleInfo objects:

try { // . . . server.registerMBean(rs, rsObjName); RoleInfo[] roleInfo = new RoleInfo[2]; roleInfo[0] = new RoleInfo( "Consumer", // role name "sample.standard.Consumer", // class name true, // role can be read true, // role can be modified 1, // must be at least one 2, // no more than two "Consumer Role Information" // description ); roleInfo[1] = new RoleInfo( "Supplier", // role name "sample.standard.Supplier", // class name true, // role can be read true, // role can be modified ...

Get Java Management Extensions now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.