2009/04/15 - Apache HiveMind has been retired.

For more information, please explore the Attic.

Jakarta > HiveMind
Jakarta
 
Font size:      

Calculator

The calculator example demonstrates the most basic concepts of HiveMind; the difference between <create-instance> and <invoke-factory>, the fact that services are, by default, created only as needed, and the ability of hivemind.BuilderFactory to automatically wire services together. It also demonstrates the behavior of the hivemind.LoggingInterceptor.

The logging configuration enables logging for the hivemind logger; that and the logging interceptors produces quite a bit of output. You can see that a proxy is created for services initially, and that the "core service implementation" for the service is created later ... the core service implementation consists of an instance of the service's POJO class, wrapped with any interceptors (the logging interceptor, in this case).

The Registry is built from the following module deployment descriptor:

<?xml version="1.0"?>
<!-- 
   Copyright 2005 The Apache Software Foundation

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
-->

<module id="examples" version="1.0.0" package="org.apache.examples">
    <service-point id="Adder">
        <create-instance class="impl.AdderImpl"/>
        <interceptor service-id="hivemind.LoggingInterceptor"/>
    </service-point>
    <service-point id="Subtracter">
        <create-instance class="impl.SubtracterImpl"/>
        <interceptor service-id="hivemind.LoggingInterceptor"/>
    </service-point>
    <service-point id="Multiplier">
        <create-instance class="impl.MultiplierImpl"/>
        <interceptor service-id="hivemind.LoggingInterceptor"/>
    </service-point>
    <service-point id="Divider">
        <create-instance class="impl.DividerImpl"/>
        <interceptor service-id="hivemind.LoggingInterceptor"/>
    </service-point>
    <service-point id="Calculator">
        <invoke-factory>
            <!-- Most properties are autowired by the BuilderFactory -->
            <construct class="impl.CalculatorImpl"/>
        </invoke-factory>
        <interceptor service-id="hivemind.LoggingInterceptor"/>
    </service-point>
    
    <service-point id="ProxyLoggingInterceptor" interface="org.apache.hivemind.ServiceInterceptorFactory">
      <create-instance class="impl.ProxyLoggingInterceptorFactory"/>
    </service-point>
    
    <service-point id="Target" interface="TargetService">
      <create-instance class="impl.TargetServiceImpl"/>
      <interceptor service-id="ProxyLoggingInterceptor"/>
    </service-point>
</module>

The service-point for the Calculator service is very simple ... as the comment indicates, the BuilderFactory is capable of locating the other services (Adder, Subtracter, etc.) by their interface, rather than requiring set-service elements to connect properites to services (using the target service's ids). These properties of the Calculator implementation are autowired to the matching services. Autowriring works only because just a single service within the entire Registry implements the specific interface. You would see errors if no service implemented the interface, or if more than one did.

This module descriptor also demonstrates two new feature of HiveMind 1.1:

Running the examples

After compiling the examples, you can use Ant to run them:

bash-2.05b$ ant run-calculator
Buildfile: build.xml

run-calculator:
     [java] Calculator [DEBUG] Creating SingletonProxy for service examples.Calculator
     [java] Inputs:   28.0 and 4.75
     [java] Calculator [DEBUG] Constructing core service implementation for service examples.Calculator
     [java] Subtracter [DEBUG] Creating SingletonProxy for service examples.Subtracter
     [java] Calculator [DEBUG] Autowired service property subtracter to <SingletonProxy for examples.Subtracter(org.apache.hivemind.examples.Subtracter)>
     [java] Divider [DEBUG] Creating SingletonProxy for service examples.Divider
     [java] Calculator [DEBUG] Autowired service property divider to <SingletonProxy for examples.Divider(org.apache.hivemind.examples.Divider)>
     [java] Multiplier [DEBUG] Creating SingletonProxy for service examples.Multiplier
     [java] Calculator [DEBUG] Autowired service property multiplier to <SingletonProxy for examples.Multiplier(org.apache.hivemind.examples.Multiplier)>
     [java] Adder [DEBUG] Creating SingletonProxy for service examples.Adder
     [java] Calculator [DEBUG] Autowired service property adder to <SingletonProxy for examples.Adder(org.apache.hivemind.examples.Adder)>
     [java] Calculator [DEBUG] Applying interceptor factory hivemind.LoggingInterceptor
     [java] Calculator [DEBUG] BEGIN add(28.0, 4.75)
     [java] Adder [DEBUG] Constructing core service implementation for service examples.Adder
     [java] Adder [DEBUG] Applying interceptor factory hivemind.LoggingInterceptor
     [java] Adder [DEBUG] BEGIN add(28.0, 4.75)
     [java] Adder [DEBUG] END add() [32.75]
     [java] Calculator [DEBUG] END add() [32.75]
     [java] Add:      32.75
     [java] Calculator [DEBUG] BEGIN subtract(28.0, 4.75)
     [java] Subtracter [DEBUG] Constructing core service implementation for service examples.Subtracter
     [java] Subtracter [DEBUG] Applying interceptor factory hivemind.LoggingInterceptor
     [java] Subtracter [DEBUG] BEGIN subtract(28.0, 4.75)
     [java] Subtracter [DEBUG] END subtract() [23.25]
     [java] Calculator [DEBUG] END subtract() [23.25]
     [java] Subtract: 23.25
     [java] Calculator [DEBUG] BEGIN multiply(28.0, 4.75)
     [java] Multiplier [DEBUG] Constructing core service implementation for service examples.Multiplier
     [java] Multiplier [DEBUG] Applying interceptor factory hivemind.LoggingInterceptor
     [java] Multiplier [DEBUG] BEGIN multiply(28.0, 4.75)
     [java] Multiplier [DEBUG] END multiply() [133.0]
     [java] Calculator [DEBUG] END multiply() [133.0]
     [java] Multiply: 133.0
     [java] Calculator [DEBUG] BEGIN divide(28.0, 4.75)
     [java] Divider [DEBUG] Constructing core service implementation for service examples.Divider
     [java] Divider [DEBUG] Applying interceptor factory hivemind.LoggingInterceptor
     [java] Divider [DEBUG] BEGIN divide(28.0, 4.75)
     [java] Divider [DEBUG] END divide() [5.894736842105263]
     [java] Calculator [DEBUG] END divide() [5.894736842105263]
     [java] Divide:   5.894736842105263

BUILD SUCCESSFUL
Total time: 3 seconds