| --- |
| title: Launching an Application after Initializing the Cache |
| --- |
| |
| <!-- |
| Licensed to the Apache Software Foundation (ASF) under one or more |
| contributor license agreements. See the NOTICE file distributed with |
| this work for additional information regarding copyright ownership. |
| The ASF licenses this file to You 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. |
| --> |
| |
| You can specify a callback application that is launched after the cache initialization. |
| |
| By specifying an `<initializer>` element in your cache.xml file, you can trigger a callback application, which is run after the cache has been initialized. Applications that use the cacheserver script to start up a server can also use this feature to hook into a callback application. To use this feature, you need to specify the callback class within the `<initializer>` element. This element should be added to the end of your `cache.xml` file. |
| |
| You can specify the `<initializer>` element for either server caches or client caches. |
| |
| The callback class must implement the `Declarable` interface. When the callback class is loaded, its `init` method is called, and any parameters defined in the `<initializer>` element are passed as properties. |
| |
| The following is an example specification. |
| |
| In cache.xml: |
| |
| ``` pre |
| <initializer> |
| <class-name>MyInitializer</class-name> |
| <parameter name="members"> |
| <string>2</string> |
| </parameter> |
| </initializer> |
| ``` |
| |
| Here's the corresponding class definition: |
| |
| ``` pre |
| |
| import org.apache.geode.cache.Declarable; |
| |
| public class MyInitializer implements Declarable { |
| public void init(Properties properties) { |
| System.out.println(properties.getProperty("members")); |
| } |
| } |
| ``` |
| |
| The following are some additional real-world usage scenarios: |
| |
| 1. Start a SystemMembershipListener |
| |
| ``` pre |
| <initializer> |
| <class-name>TestSystemMembershipListener</class-name> |
| </initializer> |
| ``` |
| |
| 2. Write a custom tool that monitors cache resources |
| |
| ``` pre |
| <initializer> |
| <class-name>ResourceMonitorCacheXmlLoader</class-name> |
| </initializer> |
| ``` |
| |
| Any singleton or timer task or thread can be instantiated and started using the initializer element. |
| |
| |