Previous Topic

Next Topic

Book Contents

Book Index

Creating Composite Producers and/or Consumers

A document on creating composite producers and/or consumers.

Composite consumers and producers have additional registration properties besides those for ordinary producers and consumers. They were listed in the Creating a Producer Service and Creating a Consumer Service parts.

Such composite services exchange data in the form of org.osgi.service.wireadmin.Envelope objects (flavors). An Envelope wraps a number of data types, for example: "front left door status", "rear left door status" and "airbag status". The org.osgi.service.wireadmin package provides the following class implementing the Envelope interface:

The following example illustrates implementing a composite producer service. The consumersConnected method is skipped because there is nothing interesting in its implementation:

                    . . .
  private String[] scope = new String[] {"current.date", "hello", "bye"};
  private Class[] flavors = new Class[] {Envelope.class};
    . . .
    
    /** Registering the service with the necessary props */
    java.util.Hashtable props = new java.util.Hashtable();
    //the data types transmitted by this producer
    props.put(WireConstants.WIREADMIN_PRODUCER_SCOPE, scope);
    props.put(org.osgi.framework.Constants.SERVICE_PID, "test.producer");
    //this property indicates the PIDs of the consumers
    //that the producer will communicate with
    props.put(WireConstants.WIREADMIN_PRODUCER_COMPOSITE, new String[] {"test.consumer"});
    //for composite services, the value of this property must be Envelope
    props.put(WireConstants.WIREADMIN_PRODUCER_FLAVORS, flavors);
    bc.registerService(Producer.class.getName(), this, props);
      . . .
    
  public Object polled(Wire wire) {  
    String date = new java.util.Date().toString();
    BasicEnvelope envelope = new BasicEnvelope(date, "test.producer", "current.date");
    System.out.println("Message from the producer: My current date is " +
                       envelope.getValue());
    return envelope;
  }