In the previous posts (
Extend ServiceMix Management features using Spring - Part 1 and
Part 2) I presented how to use spring to gain control over endpoint lifecycle and configuration via jmx. You might wonder till now
"what happens to those custom changes if I have to redeploy the assembly, restart servicemix or even worse restart the server?". The short answer is that these changes are lost. The long answer is in this blog post, which explaings how to persist those changes and how to make the endpoint reload them
each time it starts.
In order to persist and auto load custom configuration upon endpoint start-up all we need is:
For persisting
- A way to serialize the configuration in xml (jaxb2).
- A way to persist the configuration (jpa/hibernate).
For auto loading
- A way to intercept endpoint start and activate methods (spring aop).
- A way to apply that configuration to the endpoint (beanutils).
The basic idea is that for each endpoint, the custom configuration can be serialized to xml and persisted and with the use of aop interceptors reloaded to the endpoint each time it starts up.
Step 1:Configuring persistence
For persisting configuration I am going to use JPA/Hibernate and MySQL.
I want to keep things as simple as possible, so I will create a table that will only contains 2 fields
- ID, the id of the endpoint which will be the primary key.
- CONFIGURATION, a text field that will hold the configuration in xml format.
This could look like this
The endpoint id can be retrieved by calling
endpoint.getKey(). The configuration is the XML representation of the configuration
(more details later).
The persistence unit, the entity and the data access object are things that we want to be reusable so they better be in a separate jar. I will call this management-support.
Let's start creating the new jar by adding the entity.
Now we can create the persistence unit. Note that in this example I am adding all the database connection information inside persistence.xml leaving pooling to hibernate. It would be better if I created a datasource, but for the shake of simplicity I will not.
Now its time to create a very simple dao for the EndpointConfiguration entity.
Step 2:Configuring Configuration Serialization
For each endpoint type that we want its configuration to be serialized and persistence I am going to create a pojo that contains all the properties that are managed. The pojo will be annotated with Jaxb annotations so that we can easily serialize it to xml. Before serialization takes place the pojo needs to be set the values of the current configuration. For this purpose I am going to use BeanUtils
(spring beanutils). Now we can update our endpoint manager and add 2 methods
(save & load of configuration) and the ConfigurationDao that was presented above.
The new endpoint manager will expose to the jmx the saveConfiguration and loadConfiguration managed operation.
I hope that you find it useful. Personally, I've been using it for quite some time now and I am very happy with it. Using this you can even alter the xslt of an xslt-endpoint using the jmx console without having to recompile, redeploy or restart your assmebly.