@Override public void start() { try { if (this.encoder == null) { addStatus(new ErrorStatus("No encoder set for the appender named \"" + name + "\".", this)); return; } close(); this.sqs = new AmazonSQSAsyncClient(getCredentials(), this.threadPool > 0 ? Executors.newFixedThreadPool(this.threadPool) : Executors.newCachedThreadPool()); this.sqs.setEndpoint(new URI(this.queueUrl).getHost()); this.encoder.init(new SqsOutputStreamAdapter()); super.start(); } catch (Exception e) { addError(this.getClass() + " start failure", e); } }
@Override public void initialize(JSONObject config) throws Exception { String endpoint = config.getString(PARAM_ENDPOINT); String queueName = config.getString(GenericQueue.PARAM_NAME); String accessKey = config.getString(PARAM_ACCESS_KEY); String secretKey = config.getString(PARAM_SECRET_KEY); if(accessKey==null) throw new Exception(PARAM_ACCESS_KEY+" is required!"); if(secretKey==null) throw new Exception(PARAM_SECRET_KEY+" is required!"); try { this.client = new AmazonSQSAsyncClient(new BasicAWSCredentials(accessKey,secretKey)); this.client.setEndpoint(endpoint); CreateQueueRequest createQueueRequest = new CreateQueueRequest(queueName); CreateQueueResult createQueueResult = this.client.createQueue(createQueueRequest); this.queueUrl = createQueueResult.getQueueUrl(); } catch (Throwable e){ throw new Exception(e); } }
@Override protected void configure() { Region region = Region.getRegion(config.get(Regions.class, REGION_CONFIG_KEY)); AmazonSQSAsyncClient amazonSQSAsyncClient = new AmazonSQSAsyncClient(); amazonSQSAsyncClient.setRegion(region); bind(AmazonSQSAsyncClient.class).toInstance(amazonSQSAsyncClient); // Thread pool setup. Include a queue the size of the pool. // Always add headroom for admin in QueueManager int numWorkers = config.get(Integer.class, NUM_THREADS_CONFIG_KEY); int poolThreads = numWorkers + QUEUE_MANAGER_HEADROOM; ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(poolThreads, poolThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(numWorkers)); threadPoolExecutor.prestartAllCoreThreads(); bind(ExecutorService.class).toInstance(threadPoolExecutor); bind(ThreadPoolExecutor.class).toInstance(threadPoolExecutor); bind(Dispatcher.class).asEagerSingleton(); bind(Enqueuer.class).asEagerSingleton(); bind(QueueManager.class).asEagerSingleton(); }
@Test public void configuration_withMinimalBeans_shouldStartSqsListenerContainer() throws Exception { // Arrange & Act AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MinimalConfiguration.class); SimpleMessageListenerContainer container = applicationContext.getBean(SimpleMessageListenerContainer.class); // Assert assertTrue(container.isRunning()); QueueMessageHandler queueMessageHandler = applicationContext.getBean(QueueMessageHandler.class); assertTrue(QueueMessageHandler.class.isInstance(queueMessageHandler)); HandlerMethodReturnValueHandler sendToReturnValueHandler = queueMessageHandler.getCustomReturnValueHandlers().get(0); QueueMessagingTemplate messagingTemplate = (QueueMessagingTemplate) ReflectionTestUtils.getField(sendToReturnValueHandler, "messageTemplate"); AmazonSQSBufferedAsyncClient amazonBufferedSqsClient = (AmazonSQSBufferedAsyncClient) ReflectionTestUtils.getField(messagingTemplate, "amazonSqs"); AmazonSQSAsyncClient amazonSqsClient = (AmazonSQSAsyncClient) ReflectionTestUtils.getField(amazonBufferedSqsClient, "realSQS"); assertNotNull(ReflectionTestUtils.getField(amazonSqsClient, "awsCredentialsProvider")); }
@Test public void parseInternal_minimalConfiguration_createsBufferedClientWithoutExplicitTaskExecutor() throws Exception { //Arrange DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory); //Act reader.loadBeanDefinitions(new ClassPathResource(getClass().getSimpleName() + "-minimal.xml", getClass())); //Assert AmazonSQSBufferedAsyncClient sqsBufferedAsyncClient = beanFactory.getBean("customClient", AmazonSQSBufferedAsyncClient.class); AmazonSQSAsyncClient asyncClient = (AmazonSQSAsyncClient) ReflectionTestUtils.getField(sqsBufferedAsyncClient, "realSQS"); assertNotNull(asyncClient); ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) ReflectionTestUtils.getField(asyncClient, "executorService"); assertEquals(50, threadPoolExecutor.getCorePoolSize()); }
@Test public void parseInternal_withCustomTasExecutor_createsBufferedClientWithCustomTaskExecutor() throws Exception { //Arrange DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory); //Act reader.loadBeanDefinitions(new ClassPathResource(getClass().getSimpleName() + "-custom-task-executor.xml", getClass())); //Assert AmazonSQSBufferedAsyncClient sqsBufferedAsyncClient = beanFactory.getBean("customClient", AmazonSQSBufferedAsyncClient.class); AmazonSQSAsyncClient asyncClient = (AmazonSQSAsyncClient) ReflectionTestUtils.getField(sqsBufferedAsyncClient, "realSQS"); assertNotNull(asyncClient); ShutdownSuppressingExecutorServiceAdapter executor = (ShutdownSuppressingExecutorServiceAdapter) ReflectionTestUtils.getField(asyncClient, "executorService"); assertSame(beanFactory.getBean("myThreadPoolTaskExecutor"), ReflectionTestUtils.getField(executor, "taskExecutor")); }
@Test public void postConstruct_queuePropertyContainsNonExistentQueue_createsQueue() throws Exception { // ARRANGE setupConfig(); sqsAsyncClientMock = createMock(AmazonSQSAsyncClient.class); for (String queueName : new String[] { "foo", "bar" }) { expect(sqsAsyncClientMock.getQueueUrl(queueName)) .andReturn(new GetQueueUrlResult().withQueueUrl(queueName + ".queueUrl")).once(); } expect(sqsAsyncClientMock.getQueueUrl("baz")).andThrow(new QueueDoesNotExistException("lol")).once(); expect(sqsAsyncClientMock.createQueue("baz")).andReturn(new CreateQueueResult().withQueueUrl("baz.queueurl")).once(); FieldUtils.writeField(this.queueManager, "sqsClient", sqsAsyncClientMock, true); setupExecutorService(); replay(configMock, sqsAsyncClientMock, executorServiceMock); // ACT this.queueManager.postConstruct(); // ASSERT verify(configMock, sqsAsyncClientMock, executorServiceMock); }
private void setupQueueClient() throws Exception { sqsAsyncClientMock = createMock(AmazonSQSAsyncClient.class); for (String queueName : new String[] { "foo", "bar", "baz"}) { expect(sqsAsyncClientMock.getQueueUrl(queueName)) .andReturn(new GetQueueUrlResult().withQueueUrl(queueName + ".queueUrl")).once(); } FieldUtils.writeField(this.queueManager, "sqsClient", sqsAsyncClientMock, true); }
@Override protected void doStart() { sqs = new AmazonSQSAsyncClient( getCredentials(), getClientConfiguration(), Executors.newFixedThreadPool(getThreadPoolSize()) ); sqs.setEndpoint(getEndpoint()); }
@Lazy @Bean(destroyMethod = "shutdown") public AmazonSQSBufferedAsyncClient amazonSQS() throws Exception { AmazonWebserviceClientFactoryBean<AmazonSQSAsyncClient> clientFactoryBean = new AmazonWebserviceClientFactoryBean<>(AmazonSQSAsyncClient.class, this.awsCredentialsProvider, this.regionProvider); clientFactoryBean.afterPropertiesSet(); return new AmazonSQSBufferedAsyncClient(clientFactoryBean.getObject()); }
@Test public void configuration_withoutAwsCredentials_shouldCreateAClientWithDefaultCredentialsProvider() throws Exception { // Arrange & Act AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(ConfigurationWithMissingAwsCredentials.class); // Assert AmazonSQSBufferedAsyncClient bufferedAmazonSqsClient = applicationContext.getBean(AmazonSQSBufferedAsyncClient.class); AmazonSQSAsyncClient amazonSqsClient = (AmazonSQSAsyncClient) ReflectionTestUtils.getField(bufferedAmazonSqsClient, "realSQS"); assertTrue(DefaultAWSCredentialsProviderChain.class.isInstance(ReflectionTestUtils.getField(amazonSqsClient, "awsCredentialsProvider"))); }
@Test public void configuration_withRegionProvider_shouldUseItForClient() throws Exception { // Arrange & Act AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(ConfigurationWithRegionProvider.class); AmazonSQSAsync bufferedAmazonSqsClient = applicationContext.getBean(AmazonSQSAsync.class); AmazonSQSAsyncClient amazonSqs = (AmazonSQSAsyncClient) ReflectionTestUtils.getField(bufferedAmazonSqsClient, "realSQS"); // Assert assertEquals("https://" + Region.getRegion(Regions.EU_WEST_1).getServiceEndpoint("sqs"), ReflectionTestUtils.getField(amazonSqs, "endpoint").toString()); }
@Test public void parseInternal_withMultipleMessagingTemplatesDefined_shouldConfigureOnlyOneSqsClientAndDecorateOnlyOnce() throws Exception { //Arrange DefaultListableBeanFactory registry = new DefaultListableBeanFactory(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(registry); //Act reader.loadBeanDefinitions(new ClassPathResource(getClass().getSimpleName() + "-multiple-templates.xml", getClass())); //Assert AmazonSQSBufferedAsyncClient amazonSqs = registry.getBean(AmazonSQSBufferedAsyncClient.class); assertTrue(ReflectionTestUtils.getField(amazonSqs, "realSQS") instanceof AmazonSQSAsyncClient); }
@Test public void parseInternal_notBuffered_createsAsyncClientWithoutBufferedDecorator() throws Exception { //Arrange DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory); //Act reader.loadBeanDefinitions(new ClassPathResource(getClass().getSimpleName() + "-not-buffered.xml", getClass())); //Assert AmazonSQSAsyncClient asyncClient = beanFactory.getBean("customClient", AmazonSQSAsyncClient.class); assertNotNull(asyncClient); assertTrue(AmazonSQSAsyncClient.class.isInstance(asyncClient)); }
/** * Creates Amazon SQS client for given endpoint using the provided credentials. * * @param awsCredentials AWS credentials with access to the endpoint, or null to use default aws credentials. * @return Amazon SQS client. */ private static AmazonSQSAsync createInstance(AWSCredentials awsCredentials) { if (awsCredentials == null) { return new AmazonSQSAsyncClient(); } else { return new AmazonSQSAsyncClient(awsCredentials); } }
public BeanstalkConnector(String login, String pass, String region) { awsCredentials = new BasicAWSCredentials(login, pass); beanstalkClient = new AWSElasticBeanstalkClient(awsCredentials); this.beanstalkEndpoint = String.format("elasticbeanstalk.%s.amazonaws.com", region); beanstalkClient.setEndpoint(beanstalkEndpoint); this.rdsEndpoint = String.format("rds.%s.amazonaws.com", region); rdsClient = new AmazonRDSClient(awsCredentials); rdsClient.setEndpoint(rdsEndpoint); this.sqsEndpoint = String.format("sqs.%s.amazonaws.com", region); sqsClient=new AmazonSQSAsyncClient(awsCredentials); sqsClient.setEndpoint(this.sqsEndpoint); }
/** * Create the async sqs client */ private AmazonSQSAsyncClient createAsyncSQSClient( final Region region, final ExecutorService executor ) { final UsergridAwsCredentialsProvider ugProvider = new UsergridAwsCredentialsProvider(); final AmazonSQSAsyncClient sqs = new AmazonSQSAsyncClient( ugProvider.getCredentials(),clientConfiguration, executor ); sqs.setRegion( region ); return sqs; }
protected AmazonSQSAsync createSQSClient() { String regionName = System.getenv("AWS_DEFAULT_REGION"); return RegionUtils.getRegion(regionName).createClient(AmazonSQSAsyncClient.class, new DefaultAWSCredentialsProviderChain(), cc); }
@Bean public AmazonSQSAsync sqsAsyncClient() { return AmazonSQSAsyncClient.asyncBuilder().build(); }
public S3Receiver(Config config, FileManager fileManager) { this.config = config; this.fileManager = fileManager; client = new AmazonSQSAsyncClient(config.getAWSCredentials()); }
/** * Creates a new {@link ReactiveSqsClient} instance pointing to the specified AWS region. * * @param region AWS region */ public ReactiveSqsClient(Regions region) { this.sqsClient = new AmazonSQSAsyncClient(new DefaultAWSCredentialsProviderChain()); this.sqsClient.setRegion(Region.getRegion(region)); }
/** * * @param sqs */ public void setSqs(AmazonSQSAsyncClient sqs) { this.sqs = sqs; }