我已将我的作业配置为远程分区,我通过kafka发送消息,从VM正在接收请求并尝试启动该步骤,但它无法找到该步骤。
看来我提供的beanFactory(applicationContext)不包含远程执行步骤。
我已经多次在https://www.youtube.com/watch?v=CYTj5YT7CZU上看过这个例子,并且错过了如何识别正确的beanFactory并为slave工作设置。
在应用程序初始化期间,将从作业库加载ClasspathXmlApplicationContextsFactoryBean,该库包含所有弹簧批作业和远程从属步骤的XML。此bean包含每个XML文件的单独资源,包括从属文件(按预期方式),并分配给applicationContext。
@Bean
public ClasspathXmlApplicationContextsFactoryBean classpathXmlApplicationContextsFactoryBean () throws IOException
{
String resourcePath = configPropertiesService
.fetchPropertyValue(PropertyValueConstants.MXARCHIVE_SKELETON_LOCATION,
PropertyValueConstants.MXARCHIVE_SKELETON_LOCATION_DEFAULT)
.getValue() + "*.xml";
logger.trace("classpathXmlApplicationContextsFactoryBean() :: {} ", resourcePath);
Resource[] resources = applicationContext.getResources(resourcePath);
ClasspathXmlApplicationContextsFactoryBean bean = new ClasspathXmlApplicationContextsFactoryBean ();
bean.setApplicationContext(applicationContext);
bean.setResources(resources);
return bean;
}
使用applicationContext初始化BeanFactoryStepLocator。
@Bean
public BeanFactoryStepLocator stepLocator() throws Exception
{
BeanFactoryStepLocator stepLocator = new BeanFactoryStepLocator ();
/* applicationContext is autowired in the postconstruct - what we do here doesn't matter
stepLocator.setBeanFactory(applicationContext); // TODO: Find the right factory
*/
return stepLocator;
};
// EDIT: added method to code listing so the calling method is visible
@Bean
public StepExecutionRequestHandler stepExecutionRequestHandler() throws Exception {
StepExecutionRequestHandler result = new StepExecutionRequestHandler();
;
result.setJobExplorer(jobExplorer);
BeanFactoryStepLocator stepLocator = stepLocator ();
result.setStepLocator(stepLocator);
return result;
}
在执行时,框架尝试调用该步骤并失败,因为该步骤在应用程序上下文(beanFactory)中不存在。
Thread-7 2019-04-23 12:47:30,343 INFO c.m.m.s.DefaultBatchConfigurer - ThreadPoolTaskExecutor status => Active threads :: 1, Total threads ::30, 3.3333333333333335 % Active.
executionContainer-C-1 2019-04-23 12:47:30,966 ERROR o.s.k.l.LoggingErrorHandler - Error while processing: ConsumerRecord(topic = job.step, partition = 0, offset = 47, CreateTime = 1556041503162, serialized key size = 4, serialized value size = 78, headers = RecordHeaders(headers = [RecordHeader(key = sequenceNumber, value = [49, 49]), RecordHeader(key = correlationId, value = [34, 49, 55, 57, 49, 58, 102, 115, 45, 115, 116, 101, 112, 48, 48, 48, 50, 46, 115, 108, 97, 118, 101, 34]), RecordHeader(key = sequenceSize, value = [49, 50]), RecordHeader(key = spring_json_header_types, value = [123, 34, 115, 101, 113, 117, 101, 110, 99, 101, 78, 117, 109, 98, 101, 114, 34, 58, 34, 106, 97, 118, 97, 46, 108, 97, 110, 103, 46, 73, 110, 116, 101, 103, 101, 114, 34, 44, 34, 115, 101, 113, 117, 101, 110, 99, 101, 83, 105, 122, 101, 34, 58, 34, 106, 97, 118, 97, 46, 108, 97, 110, 103, 46, 73, 110, 116, 101, 103, 101, 114, 34, 44, 34, 99, 111, 114, 114, 101, 108, 97, 116, 105, 111, 110, 73, 100, 34, 58, 34, 106, 97, 118, 97, 46, 108, 97, 110, 103, 46, 83, 116, 114, 105, 110, 103, 34, 125])], isReadOnly = false), key = 11, value = StepExecutionRequest: [jobExecutionId=1791, stepExecutionId=21396, stepName=fs-step0002.slave])
org.springframework.messaging.MessageHandlingException: nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'fs-step0002.slave' available
at org.springframework.integration.handler.MethodInvokingMessageProcessor.processMessage(MethodInvokingMessageProcessor.java:109) ~[spring-integration-core-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.integration.handler.ServiceActivatingHandler.handleRequestMessage(ServiceActivatingHandler.java:93) ~[spring-integration-core-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:123) ~[spring-integration-core-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:169) ~[spring-integration-core-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.integration.dispatcher.AbstractDispatcher.tryOptimizedDispatch(AbstractDispatcher.java:115) ~[spring-integration-core-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:132) ~[spring-integration-core-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:105) ~[spring-integration-core-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:73) ~[spring-integration-core-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:461) ~[spring-integration-core-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:401) ~[spring-integration-core-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:187) ~[spring-messaging-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:166) ~[spring-messaging-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:47) ~[spring-messaging-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.messaging.core.AbstractMessageSendingTemplate.send(AbstractMessageSendingTemplate.java:109) ~[spring-messaging-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.integration.endpoint.MessageProducerSupport.sendMessage(MessageProducerSupport.java:205) ~[spring-integration-core-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.integration.kafka.inbound.KafkaMessageDrivenChannelAdapter.sendMessageIfAny(KafkaMessageDrivenChannelAdapter.java:369) ~[spring-integration-kafka-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.integration.kafka.inbound.KafkaMessageDrivenChannelAdapter.access$400(KafkaMessageDrivenChannelAdapter.java:74) ~[spring-integration-kafka-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.integration.kafka.inbound.KafkaMessageDrivenChannelAdapter$IntegrationRecordMessageListener.onMessage(KafkaMessageDrivenChannelAdapter.java:431) ~[spring-integration-kafka-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.integration.kafka.inbound.KafkaMessageDrivenChannelAdapter$IntegrationRecordMessageListener.onMessage(KafkaMessageDrivenChannelAdapter.java:402) ~[spring-integration-kafka-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.doInvokeOnMessage(KafkaMessageListenerContainer.java:1224) [spring-kafka-2.2.4.RELEASE.jar:2.2.4.RELEASE]
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.invokeOnMessage(KafkaMessageListenerContainer.java:1217) [spring-kafka-2.2.4.RELEASE.jar:2.2.4.RELEASE]
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.doInvokeRecordListener(KafkaMessageListenerContainer.java:1178) [spring-kafka-2.2.4.RELEASE.jar:2.2.4.RELEASE]
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.doInvokeWithRecords(KafkaMessageListenerContainer.java:1159) [spring-kafka-2.2.4.RELEASE.jar:2.2.4.RELEASE]
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.invokeRecordListener(KafkaMessageListenerContainer.java:1099) [spring-kafka-2.2.4.RELEASE.jar:2.2.4.RELEASE]
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.invokeListener(KafkaMessageListenerContainer.java:934) [spring-kafka-2.2.4.RELEASE.jar:2.2.4.RELEASE]
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.pollAndInvoke(KafkaMessageListenerContainer.java:750) [spring-kafka-2.2.4.RELEASE.jar:2.2.4.RELEASE]
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.run(KafkaMessageListenerContainer.java:699) [spring-kafka-2.2.4.RELEASE.jar:2.2.4.RELEASE]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [na:1.8.0_162]
at java.util.concurrent.FutureTask.run(FutureTask.java:266) [na:1.8.0_162]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_162]
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'fs-step0002.slave' available
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:772) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1221) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:294) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:204) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.batch.integration.partition.BeanFactoryStepLocator.getStep(BeanFactoryStepLocator.java:34) ~[spring-batch-integration-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.batch.integration.partition.StepExecutionRequestHandler.handle(StepExecutionRequestHandler.java:58) ~[spring-batch-integration-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at sun.reflect.GeneratedMethodAccessor687.invoke(Unknown Source) ~[na:na]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_162]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_162]
at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:170) ~[spring-messaging-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:120) ~[spring-messaging-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.integration.handler.support.MessagingMethodInvokerHelper$HandlerMethod.invoke(MessagingMethodInvokerHelper.java:1119) ~[spring-integration-core-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.integration.handler.support.MessagingMethodInvokerHelper.invokeHandlerMethod(MessagingMethodInvokerHelper.java:617) ~[spring-integration-core-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.integration.handler.support.MessagingMethodInvokerHelper.processInternal(MessagingMethodInvokerHelper.java:490) ~[spring-integration-core-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.integration.handler.support.MessagingMethodInvokerHelper.process(MessagingMethodInvokerHelper.java:313) ~[spring-integration-core-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.integration.handler.MethodInvokingMessageProcessor.processMessage(MethodInvokingMessageProcessor.java:106) ~[spring-integration-core-5.1.3.RELEASE.jar:5.1.3.RELEASE]
... 29 common frames omitted
步骤XML(已清理)是:
<?xml version="1.0" encoding="UTF-8"?>
<beans default-lazy-init="false" > <!-- usual suspects -->
<bean class="org.springframework.batch.core.scope.JobScope">
<property name="proxyTargetClass" value="true" />
</bean>
<bean class="org.springframework.batch.core.scope.StepScope">
<property name="proxyTargetClass" value="true" />
</bean>
<batch:step id="fs-step0002.slave">
<batch:tasklet transaction-manager="jtaTransactionManager"
start-limit="100">
<batch:chunk reader="myCustomItemReader" writer="myCustomWriter"
commit-interval="1"/>
</batch:tasklet>
<batch:fail on="FAILED" />
</batch:step>
</beans>
编辑1:
我强制创建一个新的ClasspathXmlApplicationContextsFactoryBean,它只读取远程步骤文件(并且只读取其中一个)。它仍然抱怨没有预期名称的豆,但我可以看到它肯定存在。
后来,当我追踪到steplocator时,我可以看到beanFactory是空的。
用于测试的变通方法代码:
@Bean
public ClasspathXmlApplicationContextsFactoryBean stepFactoryBean() throws IOException {
String resourcePath = configPropertiesService
.fetchPropertyValue(PropertyValueConstants.MXARCHIVE_SKELETON_LOCATION,
PropertyValueConstants.MXARCHIVE_SKELETON_LOCATION_DEFAULT)
.getValue() + "*slave.xml";
logger.trace("classpathXmlApplicationContextsFactoryBean() :: {} ", resourcePath);
Resource[] resources = applicationContext.getResources(resourcePath);
ClasspathXmlApplicationContextsFactoryBean bean = new ClasspathXmlApplicationContextsFactoryBean();
bean.setApplicationContext(applicationContext);
bean.setResources(resources);
return bean;
}
@Bean
public BeanFactoryStepLocator stepLocator() throws Exception {
BeanFactoryStepLocator stepLocator = new BeanFactoryStepLocator();
ApplicationContextFactory[] factories = stepFactoryBean().getObject();
if (factories != null) {
stepLocator.setBeanFactory(factories[0].createApplicationContext().getBeanFactory());
}
return stepLocator;
};
在创建时,stepLocator中的beanFactory的调试器视图。
调试器在stepLocator()工厂方法结束时查看getStep()的结果:
编辑2:
似乎原始的“applicationContext”被自动装配为beanfactory,因此真正的问题是将其他bean加载到applicationContext中。
我也尝试过how to load additional bean configuration file in spring at run time建议的解决方案和相关问题。
您可以尝试声明BeanFactory所需的依赖关系,让Spring给出正确的实例,而不是手动执行查找/注入。所以代替:
@Bean
public BeanFactoryStepLocator stepLocator() throws Exception {
BeanFactoryStepLocator stepLocator = new BeanFactoryStepLocator();
ApplicationContextFactory[] factories = stepFactoryBean().getObject();
if (factories != null) {
stepLocator.setBeanFactory(factories[0].createApplicationContext().getBeanFactory());
}
return stepLocator;
};
使用类似的东西:
@Bean
public StepLocator stepLocator(BeanFactory beanFactory) {
BeanFactoryStepLocator beanFactoryStepLocator = new BeanFactoryStepLocator();
beanFactoryStepLocator.setBeanFactory(beanFactory);
return beanFactoryStepLocator;
}
希望这可以帮助。