Spring integration Spring集成-从配置文件调度作业
我使用SpringIntegration解析XML文件,需要为每个标记创建一个线程(每个线程的速率不同) 现在(在这里许多用户的帮助下:)我能够按标记分割XML,然后将其路由到相应的Spring integration Spring集成-从配置文件调度作业,spring-integration,Spring Integration,我使用SpringIntegration解析XML文件,需要为每个标记创建一个线程(每个线程的速率不同) 现在(在这里许多用户的帮助下:)我能够按标记分割XML,然后将其路由到相应的服务激活器 这非常有效,但我无法重定向到创建“线程”然后执行操作的通道。现在我有以下配置,在我的脑海中(我不知道它是否正确…) 这是我的实际配置,用于拆分标记并重定向到通道。 路由器不应该直接重定向到通道,而应该调度它们 在第一个实例中,我将足够用固定速率在池中重定向它,稍后我将使用XPATH获取属性,然后用正确的值
服务激活器
这非常有效,但我无法重定向到创建“线程”然后执行操作的通道。现在我有以下配置,在我的脑海中(我不知道它是否正确…)
这是我的实际配置,用于拆分标记并重定向到通道。
路由器不应该直接重定向到通道,而应该调度它们
在第一个实例中,我将足够用固定速率在池中重定向它,稍后我将使用XPATH获取属性,然后用正确的值替换这个“固定”速率
我尝试了许多解决方案来创建此流,但每个解决方案都失败或无法编译:(
我将有一个classe(Service
),它是处理服务标签的通用类,完成一些操作,然后“返回”值,这样我就可以重定向到另一个通道
public class Service {
public int execute() {
// Execute the task and return the value to continue the "chain"
}
}
根本不清楚您的意思是什么;您拆分了一个标记;路由了它,但希望以XML中的速率“调度”它。这里不清楚您所说的“调度”是什么意思-通常每个消息在一个调度中处理一次而不是多次 正如我所说,我不明白你需要做什么,但a可能是合适的 另一种可能性是,可以从消息中导出延迟量 编辑 因为您的“服务”似乎不接受任何输入数据,所以看起来您只需要为每个服务配置/启动一个
,然后根据XML中的参数启动它
<int:inbound-channel-adapter id="service1" channel="foo"
auto-startup="false"
ref="service1Bean" method="execute">
<poller fixed-delay="1000" />
</int:inbound-channel-adapter/>
我不知道这是否是实现流程的正确方法,但我编写了以下代码: applicationContext.xml
<context:component-scan base-package="it.mypkg" />
<!-- Expression to extract interval from XML tag -->
<si-xml:xpath-expression id="selectIntervalXpath" expression="//*/@interval" />
<si:channel id="rootChannel" />
<!-- Split each tag to redirect on router -->
<si-xml:xpath-splitter id="mySplitter" input-channel="rootChannel" output-channel="routerChannel" create-documents="true">
<si-xml:xpath-expression expression="//service|//activity" />
</si-xml:xpath-splitter>
<!-- Route each tag to the appropiate channel -->
<si-xml:xpath-router id="router" input-channel="routerChannel" evaluate-as-string="true">
<si-xml:xpath-expression expression="concat(name(./node()), 'Channel')" />
</si-xml:xpath-router>
<!-- Activator for Service Tag -->
<si:service-activator input-channel="serviceChannel" method="schedule">
<bean class="it.mypkg.Service" />
</si:service-activator>
<!-- Activator for Activity Tag -->
<si:service-activator input-channel="activityChannel" method="schedule">
<bean class="it.mypkg.Activity" />
</si:service-activator>
<!-- Task scheduler -->
<task:scheduler id="taskScheduler" pool-size="10"/>
还有一个服务类示例(处理.xml上提供的所有标记Service
)
现在,为了继续我的流程,我需要找到一种方法,将每个作业的输出重定向到Spring Integration(applicationContext.xml)。我知道有点难以解释我必须做什么……不过,我用一个小例子更新了我的问题。现在,我将查看您的链接我想到的是“拆分”标记并提取间隔。这个间隔将需要明天提交任务(由ScheduledExecutorService-scheduleAtFixedRate完成),我将研究它,现在我正在尝试使用delayer和xpath transformer(提取标记中的属性间隔)但我希望…不起作用:D所以,使用您给我提供的示例,我可以使用interval(从标记中提取)的值作为固定延迟的属性?此外,通过这种方式,我将有不同的类(服务、活动等)来处理标记,对吗?标记可以有其他属性,因此我必须找到一种不“丢失”的方法节点…(服务可以具有执行所需的其他属性…)
public class Service {
public int execute() {
// Execute the task and return the value to continue the "chain"
}
}
<int:inbound-channel-adapter id="service1" channel="foo"
auto-startup="false"
ref="service1Bean" method="execute">
<poller fixed-delay="1000" />
</int:inbound-channel-adapter/>
@Autowired
SourcePollingChannelAdapter service1;
...
public void startService1(Node node) {
...
service1.setTrigger(new PeridicTrigger(...));
service1.start();
...
}
<context:component-scan base-package="it.mypkg" />
<!-- Expression to extract interval from XML tag -->
<si-xml:xpath-expression id="selectIntervalXpath" expression="//*/@interval" />
<si:channel id="rootChannel" />
<!-- Split each tag to redirect on router -->
<si-xml:xpath-splitter id="mySplitter" input-channel="rootChannel" output-channel="routerChannel" create-documents="true">
<si-xml:xpath-expression expression="//service|//activity" />
</si-xml:xpath-splitter>
<!-- Route each tag to the appropiate channel -->
<si-xml:xpath-router id="router" input-channel="routerChannel" evaluate-as-string="true">
<si-xml:xpath-expression expression="concat(name(./node()), 'Channel')" />
</si-xml:xpath-router>
<!-- Activator for Service Tag -->
<si:service-activator input-channel="serviceChannel" method="schedule">
<bean class="it.mypkg.Service" />
</si:service-activator>
<!-- Activator for Activity Tag -->
<si:service-activator input-channel="activityChannel" method="schedule">
<bean class="it.mypkg.Activity" />
</si:service-activator>
<!-- Task scheduler -->
<task:scheduler id="taskScheduler" pool-size="10"/>
public abstract class Operation {
protected TaskScheduler taskScheduler;
protected XPathExpression selectIntervalXpath;
abstract public void schedule(Node document);
@Autowired
public void setTaskScheduler(TaskScheduler taskScheduler) {
this.taskScheduler= taskScheduler;
}
public TaskScheduler getTaskScheduler() {
return this.taskScheduler;
}
@Autowired
public void setSelectIntervalXpath(XPathExpression selectIntervalXpath) {
this.selectIntervalXpath = selectIntervalXpath;
}
public XPathExpression getSelectIntervalXPath() {
return this.selectIntervalXpath;
}
}
public class Service extends Operation {
private static final Logger log = Logger.getLogger(Service.class);
@Override
public void schedule(Node document) {
log.debug("Scheduling Service");
long interval = Long.parseLong(this.selectIntervalXpath.evaluateAsString(document));
this.taskScheduler.scheduleAtFixedRate(new ServiceRunner(), interval);
}
private class ServiceRunner implements Runnable {
public void run() {
log.debug("Running...");
}
}
}