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 1, // must be at least one 1, // no more than one "Supplier Role Information" // description ); } catch (Exception e) { // . . . }
In this case, the relation consists of two roles,
Consumer
and Supplier
,
performed by two MBean classes, also called
Consumer
and Supplier
(in the
sample application, we reuse the MBean classes from the
standard
package). There must be at least one and
no more than two Consumer
MBeans in the relation.
Only one Supplier
MBean is allowed in the
relation. Once the roles have been described using
RoleInfo
objects, we are ready to describe the
relationship between these two roles by creating an internal relation
type:
try { // . . . roleInfo[1] = new RoleInfo( "Supplier", // role name "sample.standard.Supplier", // class name true, // role can be read true, // role can be modified 1, // must be at least one 1, // no more than one "Supplier Role Information" // description ); rs.createRelationType( "ConsumerSupplierRelationType_Internal", roleInfo ); // . . . } catch (Exception e) { // . . . }
Once the relation type has been created, we instantiate the role by
creating a Role
object for each group of MBeans to
participate in the relation:
try { // . . . rs.createRelationType( "ConsumerSupplierRelationType_Internal", roleInfo ); // Create and register a Consumer MBean ObjectName consumerObjName = createWorker("Consumer", 100); ArrayList consumerList = new ArrayList( ); consumerList.add(consumerObjName); Role consumerRole = new Role("Consumer", consumerList); // Create and register a Supplier MBean ObjectName supplierObjName = createWorker("Supplier", 100); ArrayList supplierList = new ArrayList( ); supplierList.add(supplierObjName); Role supplierRole = new Role("Supplier", supplierList); RoleList roles = new RoleList( ); roles.add(consumerRole); roles.add(supplierRole); // . . . } catch (Exception e) { // . . . }
In this example, we use the createWorker( )
method to create and register with the MBean server an instance of
each worker type. Once the Role
objects are
created, we create a RoleList
object to contain
the Role
objects. The final step is to use the
relation service to create an internal relation:
try { // . . . RoleList roles = new RoleList( ); roles.add(consumerRole); roles.add(supplierRole); rs.createRelation( "ConsumerSupplierRelation_Internal", "ConsumerSupplierRelationType_Internal", roles ); } catch (Exception e) { // . . . }
Example 11-1 shows a complete source listing of how
to create the internal
Consumer
/Supplier
relation
we’ve
been
examining.
Example 11-1. Creating an internal relation
try { MBeanServer server = MBeanServerFactory.createMBeanServer( ); boolean purgeImmediate = true; RelationService rs = new RelationService(purgeImmediate); ObjectName rsObjName = new ObjectName("AgentServices:name=Relation"); 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 1, // must be at least one 1, // no more than one "Supplier Role Information" // description ); rs.createRelationType( "ConsumerSupplierRelationType_Internal", roleInfo ); // Create and register a Consumer MBean ObjectName consumerObjName = createWorker("Consumer", 100); ArrayList consumerList = new ArrayList( ); consumerList.add(consumerObjName); Role consumerRole = new Role("Consumer", consumerList); // Create and register a Supplier MBean ObjectName supplierObjName = createWorker("Supplier", 100); ArrayList supplierList = new ArrayList( ); supplierList.add(supplierObjName); Role supplierRole = new Role("Supplier", supplierList); RoleList roles = new RoleList( ); roles.add(consumerRole); roles.add(supplierRole); rs.createRelation( "ConsumerSupplierRelation_Internal", "ConsumerSupplierRelationType_Internal", roles ); } catch (Exception e) { // . . . }