Home > Back-end >  What message handler to use for modifying "static data" maintained within a singleton bean
What message handler to use for modifying "static data" maintained within a singleton bean

Time:09-17

I'm using a singleton bean to hold certain "static data" that is evaluated at the API start and may get updated from time to time.

I'm setting the bean initially as a map in the context:

<util:map id="staticData" key-type="java.lang.String" >
    <entry key="keyA" value="#{null}" />
    <entry key="keyB" value="#{null}" />
</util:map>

The staticData values are then updated periodically. For the updates, I'm currently using header-enricher, for lack of a better idea.

<int:header-enricher>
    <si:header name="dummyHeader" expression="@staticData.put('keyA', payload)" />
</int:header-enricher>

This "works" but seems inelegant and also creates a dummyHeader with no utility.

Would you recommend a better approach to this mechanism?

Thanks for any pointers and best regards

CodePudding user response:

This sounds more like just a service-activator:

<service-activator output-channel="nullChannel" expression="@staticData.put('keyA', payload)" />

Indeed header-enricher is about modifying a request message for downstream processing.

What you need is just update external store and forget. Of course you may have a requirements to store that data there and continue your flow. For that purpose you can use a <publish-subscribe-channel> and have two subscribers where one is exactly that service-activator.

We have to use that output-channel="nullChannel" to ignore a result of the Map.put() call.

See more info in docs: https://docs.spring.io/spring-integration/docs/current/reference/html/messaging-endpoints.html#service-activator

  • Related