Java Generate/Merge Files(4)Spring and AWS SQS
云英才
2023-12-01
Java Generate/Merge Files(4)Spring and AWS SQS
Dependencies
<!-- AWS Connections -->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
<version>1.11.113</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-sqs</artifactId>
<version>1.11.113</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>amazon-sqs-java-messaging-lib</artifactId>
<version>1.0.1</version>
<exclusions>
<exclusion>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-sqs</artifactId>
</exclusion>
</exclusions>
</dependency>
How to use JMS Configuration with Spring to Send message?
<!-- JMS -->
<bean id="credentialsProviderBean" class="com.j2c.feeds2g.services.base.J2CAWSCredentialsProviderChain" />
<bean id="connectionFactoryBuilder"
class="com.amazon.sqs.javamessaging.SQSConnectionFactory$Builder">
<property name="regionName" value="us-east-1" />
<property name="numberOfMessagesToPrefetch" value="10" />
<property name="awsCredentialsProvider" ref="credentialsProviderBean" />
</bean>
<bean id="connectionFactory" class="com.amazon.sqs.javamessaging.SQSConnectionFactory"
factory-bean="connectionFactoryBuilder" factory-method="build" />
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory" />
</bean>
Java MessageServiceImpl to send out the messages
package com.j2c.feeds2g.services;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import com.j2c.feeds2g.services.base.BaseService;
public class MessageServiceImpl extends BaseService implements MessageService{
private JmsTemplate jmsTemplate;
public void sendJobBucket(String jobBucket) {
jmsTemplate.send("test-jobbucket", new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
final TextMessage msg = session.createTextMessage(jobBucket);
return msg;
}
});
}
public void setJmsTemplate(JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
}
}
In the configuration file, we need to have the key and secret for SQS
aws.accessKeyId=xxxxxx
aws.secretKey=xxxxxx
How to use Message Driven Bean to consume the messages?
<!-- MDB for jobbucket queue -->
<bean id="amazonMessageListener" class="com.j2c.feeds2g.services.mdb.JobBucketMessageDrivenBean">
<property name="actorSystemFactory" ref="actorSystemFactory" />
</bean>
<bean id="messageListener"
class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
<property name="delegate" ref="amazonMessageListener" />
<property name="defaultListenerMethod" value="onMessage" />
<property name="messageConverter">
<null />
</property>
</bean>
<bean id="jmsContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destinationName" value="test-jobbucket" />
<property name="messageListener" ref="messageListener" />
</bean>
<!-- MDB end -->
package com.j2c.feeds2g.services.mdb;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.TextMessage;
import com.j2c.feeds2g.services.base.ActorSystemFactory;
import com.j2c.feeds2g.services.base.BaseService;
import akka.actor.ActorSelection;
import akka.actor.ActorSystem;
public class JobBucketMessageDrivenBean extends BaseService{
private ActorSystemFactory actorSystemFactory;
private ActorSelection sourceRouter;
public void onMessage(Message message) {
logger.info("step1 receive information");
if(sourceRouter == null){
ActorSystem system = actorSystemFactory.getActorSystem();
sourceRouter = system.actorSelection("akka://feeds2g/user/sourceRouter");
}
if (message instanceof TextMessage) {
try {
logger.info(String.format("MDB received: %s ", ((TextMessage) message).getText()));
//sourceRouter.tell(null, null);
} catch (JMSException ex) {
throw new RuntimeException(ex);
}
} else {
throw new IllegalArgumentException("Message must be of type TextMessage");
}
}
public void setActorSystemFactory(ActorSystemFactory actorSystemFactory) {
this.actorSystemFactory = actorSystemFactory;
}
}
References:
https://pragmaticintegrator.wordpress.com/2015/04/21/using-aws-sqs-as-jms-provider-with-spring/
https://pragmaticintegrator.wordpress.com/2015/05/05/creating-a-message-driven-bean-with-aws-sqs-in-spring/
https://codingbone.wordpress.com/2010/02/28/how-to-load-properties-files-into-spring-and-expose-to-the-java-classes/