我已经创建了一个Spring Boot JMS应用程序来接收来自特定amazon sqs队列的消息。作为Java应用程序运行,它无故障地接收消息。 但是,当我在wildfly v10.0.0服务器上部署时,它无效。
所以,这是我的Application类:
@SpringBootApplication
@EnableJms
public class QueueListenerFadeApplication {
private SQSConnectionFactory connectionFactory;
@Value("${aws.access.key}")
private String accessKey;
@Value("${aws.secret.key}")
private String secretKey;
private static final Logger logger =
Logger.getLogger(QueueListenerFadeApplication.class.getName());
@Bean
public JmsListenerContainerFactory<?> myFactory() {
final DefaultJmsListenerContainerFactory factory = new
DefaultJmsListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
return factory;
}
private SQSConnectionFactory createSQSConnectionFactory() {
BasicAWSCredentials awsCredentials = new
BasicAWSCredentials(this.accessKey, this.secretKey);
final AmazonSQS sqs = AmazonSQSClient.builder()
.withRegion(Regions.AP_SOUTH_1)
.withCredentials(new
AWSStaticCredentialsProvider(awsCredentials))
.build();
return new SQSConnectionFactory(new ProviderConfiguration(), sqs);
}
@PostConstruct
public void init() {
this.connectionFactory = createSQSConnectionFactory();
}
public static void main(String[] args) {
logger.log(Level.INFO, "Starting Queue Listener...");
SpringApplication.run(QueueListenerFadeApplication.class, args);
}
}
这是我的队列侦听器类:
@Component
public class QueueListenerReceiveService {
private ObjectMapper objectMapper;
private static final Logger logger =
Logger.getLogger(QueueListenerReceiveService.class.getName());
@JmsListener(destination = "${sqs.queue.name}", containerFactory="myFactory")
public void receiveMessage(@Headers Map<String, Object> messageAttributes, @Payload String json) {
try {
objectMapper = new ObjectMapper();
SQSMessage sqsMessage = objectMapper.readValue(json, SQSMessage.class);
logger.log(Level.INFO, "Message Received=" + sqsMessage);
FadeStarterService.start(sqsMessage.getUseCase(), sqsMessage.getZoneId());
} catch (IOException e) {
e.printStackTrace();
}
}
}
我正在使用Maven来管理依赖项,所以这里是pom.xml中的依赖项
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk</artifactId>
<version>1.11.311</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>amazon-sqs-java-messaging-lib</artifactId>
<version>1.0.4</version>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
</dependencies>
最后是pom.xml中的build标签
<build>
<finalName>queue-listener-fade</finalName>
<plugins>
<plugin>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<version>1.0.1.Final</version>
<configuration>
<hostname>locahost</hostname>
<port>9990</port>
<username>deplyusr</username>
<password>pssdploy</password>
</configuration>
</plugin>
</plugins>
</build>