How to name and initialize a class of a service activator dynamically

我们两清 提交于 2019-12-12 03:14:58

问题


This is follow-up on my earlier question How to set payload as constructor-arg value in service-activator.

We've 12 different classes like AProcessor, Bprocessor and so on.. and the each constructor accept common payload and execute same declared method in them 'publish'.

Now, I was thinking to have single common channel and service activator in SI which can handle this but then how can I put the class name in the expression dynamically which I can derive from the payload.type.

I was trying something like below but its not working

<service-activator input-channel="COMMON_PUBLISH_CHANNEL" expression="'new mypackage.'+payload.type+ 'RequestProcessor'(payload.myservice).publish(payload.data)"> </service-activator>

I want to save from writing 12 channels and service activators if I can do at one place.


回答1:


I'd suggests to move such a logic into Java class and still have one <service-activator>. All the hard work to decide which target processor to invoke should be done there, in the service-method.

Spring Integration with its power to be so flexible with all those components and channels between them may lead to the misleading that it is programming language. When the real language is Java and we shouldn't forget its generic functionality.

Although we can achieve your wishes with Spring Integration anyway using SpEL magic:

<chain>
   <header-enricher name="service" expression="@serviceRegistry[payload.type]"/>
   <service-activator expression="headers[service].process(payload)"/>
<chain>


来源:https://stackoverflow.com/questions/39171203/how-to-name-and-initialize-a-class-of-a-service-activator-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!