我有一个Spring Boot项目,用于从Amazon SQS队列接收事件。我一直在使用Spring Cloud AWS项目来简化这个过程。
问题是:Spring Boot应用程序启动得很好,并且似乎很好地实例化了所有必需的bean。但是,当调用用SqsListener注释的方法时,事件处理程序的所有依赖bean都为null。
@Service("systemEventsHandler")
public class SystemEventsHandler {
// A service that this handler depends on
private CustomService customService;
private ObjectMapper objectMapper;
@Autowired
public SystemEventsHandler(CustomService customService, ObjectMapper objectMapper) {
this.matchStatusSvc = matchStatusSvc;
this.objectMapper = objectMapper;
}
public void handleEventFromHttpCall(CustomEventObject event) {
// Whenever this method is called, the customService is
// present and the method call completes just fine.
Assert.notNull(objectMapper, "The objectMapper that was injected was null");
customService.handleEvent(event);
}
@SqsListener(value = "sqsName", deletionPolicy = SqsMessageDeletionPolicy.ON_SUCCESS)
private void handleEventFromSQSQueue(@NotificationMessage String body) throws IOException {
// Whenever this method is called, both the objectMapper and
// the customService are null, causing the invocation to
// fail with a NullPointerException
CustomEventObject event = objectMapper.readValue(body, CustomEventObject.class);
matchStatusSvc.scoresheetUploaded(matchId);
}
}
java prettyprint-override">@RestController
@RequestMapping("/events")
public class SystemEventsController {
private final SystemEventsHandler sysEventSvc;
@Autowired
public SystemEventsController(SystemEventsHandler sysEventSvc) {
this.sysEventSvc = sysEventSvc;
}
@RequestMapping(value = "", method = RequestMethod.POST)
public void handleCustomEvent(@RequestBody CustomEventObject event) {
sysEventSvc.handleEventFromHttpCall(event);
}
}
相关配置:
@Configuration
public class AWSSQSConfig {
@Bean
public SimpleMessageListenerContainer simpleMessageListenerContainer(AmazonSQSAsync amazonSQS) {
SimpleMessageListenerContainer msgListenerContainer = simpleMessageListenerContainerFactory(amazonSQS).createSimpleMessageListenerContainer();
msgListenerContainer.setMessageHandler(queueMessageHandler(amazonSQS));
return msgListenerContainer;
}
@Bean
public SimpleMessageListenerContainerFactory simpleMessageListenerContainerFactory(AmazonSQSAsync amazonSQS) {
SimpleMessageListenerContainerFactory msgListenerContainerFactory = new SimpleMessageListenerContainerFactory();
msgListenerContainerFactory.setAmazonSqs(amazonSQS);
msgListenerContainerFactory.setMaxNumberOfMessages(10);
msgListenerContainerFactory.setWaitTimeOut(1);
return msgListenerContainerFactory;
}
@Bean
public QueueMessageHandler queueMessageHandler(AmazonSQSAsync amazonSQS) {
QueueMessageHandlerFactory queueMsgHandlerFactory = new QueueMessageHandlerFactory();
queueMsgHandlerFactory.setAmazonSqs(amazonSQS);
QueueMessageHandler queueMessageHandler = queueMsgHandlerFactory.createQueueMessageHandler();
return queueMessageHandler;
}
@Bean(name = "amazonSQS", destroyMethod = "shutdown")
public AmazonSQSAsync amazonSQSClient() {
AmazonSQSAsyncClient awsSQSAsyncClient = new AmazonSQSAsyncClient(new DefaultAWSCredentialsProviderChain());
return awsSQSAsyncClient;
}
}
其他信息:
有什么想法吗?
正如spencergibb在上面的评论中所建议的那样,将方法的可见性从私有改为公共起到了作用。
我面对的是Bean(...)没有资格被所有BeanPostProcess处理。我已经将问题缩小到@Configuration类中的@Bean方法,我认为这是罪魁祸首: 配置类 据我所知,BeanPostProcessor bean创建得相当快,需要过早地实例化MeterRegistry bean,这使得它不合格(因为BeanPostProcessor正在操作尚未注入的bean)。 我在博客上看到一
Gradle 支持从 Maven 或 Ivy 仓库中拉取依赖文件。首先必须将仓库添加到列表中,然后必须在 dependencies 中添加 Maven 或 Ivy 声明的包。 repositories { jcenter() } dependencies { compile 'com.google.guava:guava:18.0' } android { ... } 注意
配置 jar 包需要在 compile 中添加响应依赖。下面的代码添加了 libs 文件夹中的所有 jar 作为依赖。 dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) } android { ... } 注意:dependencies DSL 标签是标准 Gradle API 中的一部分,所以它不属于
我有一个基本的Spring Boot项目,在这个项目中,我试图用RabbitMQ实现简单的消息传递。当我将spring-boot-starter-amqp依赖项添加到我的文件并启动Spring Boot应用程序时,我会得到导致的异常: pom.xml 如果我从pom.xml中移除spring-boot-starter-amqp依赖项,项目将不会出错:
在一个无状态bean中,我正在查找队列,这是正确的,并且发送没有抛出异常: 但不调用消息驱动bean的onMessage方法。 我错过了什么? 我的消息驱动bean有以下代码:
我正在使用android Studio V1.5构建工具23.0.2和gradle V2.8构建一个android应用程序。此ADT包于2016年1月下载。我的应用程序依赖于ustilies.aar。它被添加到应用程序的build.gradle中,使用网页中的代码片段,如下所示 Android Studio没有显示任何错误。它编译成一个apk fine,但当我运行它时,该应用程序立即崩溃,。 gr