B4J Question AWS SNS

aaronk

Well-Known Member
Licensed User
Longtime User
you do need to find the SDK with the jars. I don't see it.
I think it's available on GitHub..

Not 100% sure on which jar files are required though to make this work. (I think it's the files from the above link)
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
'Non-UI application (console / server application)
#Region Project Attributes 
    #CommandLineArgs:
    #MergeLibraries: False 
#End Region
#AdditionalJar: aws-java-sdk-1.11.914.jar
#AdditionalJar: aspectjrt-1.8.2.jar
#AdditionalJar: aspectjweaver.jar
#AdditionalJar: aws-swf-build-tools-1.1.jar
#AdditionalJar: freemarker-2.3.9.jar
#AdditionalJar: javax.mail-api-1.4.6.jar
#AdditionalJar: spring-beans-3.0.7.RELEASE.jar
#AdditionalJar: spring-context-3.0.7.RELEASE.jar
#AdditionalJar: spring-core-3.0.7.RELEASE.jar
#AdditionalJar: spring-test-3.0.7.RELEASE.jar
#AdditionalJar: jmespath-java-1.11.914.jar
#AdditionalJar: netty-buffer-4.1.53.Final.jar
#AdditionalJar: netty-codec-4.1.53.Final.jar
#AdditionalJar: netty-codec-http-4.1.53.Final.jar
#AdditionalJar: netty-common-4.1.53.Final.jar
#AdditionalJar: netty-handler-4.1.53.Final.jar
#AdditionalJar: netty-resolver-4.1.53.Final.jar
#AdditionalJar: netty-transport-4.1.53.Final.jar
#AdditionalJar: jackson-databind-2.6.7.4.jar
#AdditionalJar: httpclient-4.5.13.jar
#AdditionalJar: httpcore-4.4.13.jar
#AdditionalJar: commons-codec-1.11.jar
#AdditionalJar: commons-logging-1.1.3.jar
#AdditionalJar: ion-java-1.0.2.jar
#AdditionalJar: jackson-annotations-2.6.0.jar
#AdditionalJar: jackson-core-2.6.7.jar
#AdditionalJar: jackson-dataformat-cbor-2.6.7.jar
#AdditionalJar: joda-time-2.8.1.jar

Sub Process_Globals
    
End Sub

Sub AppStart (Args() As String)
    Dim jo As JavaObject = Me
    jo.RunMethod("SendSms", Array("access key", "secret access key", "message", "+23423423"))
    StartMessageLoop
End Sub

#if Java
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.sns.AmazonSNS;
import com.amazonaws.services.sns.AmazonSNSClient;
import com.amazonaws.services.sns.model.PublishRequest;

public static void SendSms(String AccessKey, String SecretAccessKey, String Message, String MobileNumber) {
    BasicAWSCredentials awsCreds = new BasicAWSCredentials(AccessKey, SecretAccessKey);
        
        //Create SNS Client
        AmazonSNS snsClient = AmazonSNSClient
                .builder()
                .withRegion(Regions.AP_SOUTHEAST_2)
                .withCredentials(new AWSStaticCredentialsProvider(awsCreds))
                .build();
        
        String SMSMessage = Message;
        String mobile = MobileNumber;
        
        snsClient.publish(new PublishRequest().withMessage(SMSMessage).withPhoneNumber(mobile));
}
#End If

1. Unzip the SDK and copy aws-java-sdk-1.11.914.jar + all the third party jars to the additional libraries folder.
2. Use the code above. Make sure to set #MergeLibraries to False as it is a huge jar.
 
Upvote 0

Bill Milleker

New Member
Licensed User
In the AWS SDK for Java 2.x, they now use individual jar files. The advantage is that the size is now small enough to merge in the libraries. The disadvantage is that the build process is based on Maven, so it takes some investigation to include the required jar files.

Below is Erel's example converted to AWS SDK for Java 2.x. It requires at least Java 11.

B4X:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
    #CommandLineArgs:
    #MergeLibraries: True
#End Region

'select JavaObject library for this project
'requires Java 11 due to static interface method invocations

'get jar files from https://mvnrepository.com/artifact/software.amazon.awssdk/
'put into AdditionalLibraries directory
#AdditionalJar: sdk-core-2.16.5.jar
#AdditionalJar: auth-2.16.5.jar
#AdditionalJar: regions-2.16.5.jar
#AdditionalJar: protocol-core-2.16.5.jar
#AdditionalJar: apache-client-2.16.5.jar
#AdditionalJar: utils-2.16.5.jar
#AdditionalJar: http-client-spi-2.16.5.jar
#AdditionalJar: aws-core-2.16.5.jar
#AdditionalJar: metrics-spi-2.16.5.jar
#AdditionalJar: profiles-2.16.5.jar
#AdditionalJar: aws-query-protocol-2.16.5.jar
'include service-specific jar files as needed
#AdditionalJar: sns-2.16.5.jar

'get these dependencies from https://mvnrepository.com/
#AdditionalJar: reactive-streams-1.0.3.jar
#AdditionalJar: httpclient-4.5.13.jar
#AdditionalJar: httpcore-4.4.14.jar
#AdditionalJar: commons-logging-1.2.jar
#AdditionalJar: slf4j-api-1.7.30.jar
#AdditionalJar: slf4j-simple-1.7.30.jar
'#AdditionalJar: slf4j-nop-1.7.30.jar

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    Private Button1 As B4XView
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
    DateTime.DateFormat="yyyy-MM-dd HH:mm:ss"
End Sub

Sub Button1_Click
    Dim jo As JavaObject = Me
    jo.RunMethod("SendSms", Array("access key", "secret access key", DateTime.Date(DateTime.Now), "+12025550113"))
    xui.MsgboxAsync("Message sent!", "B4X")
End Sub

#if Java
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.sns.SnsClient;
import software.amazon.awssdk.services.sns.model.PublishRequest;
import software.amazon.awssdk.services.sns.model.PublishResponse;

public static void SendSms(String AccessKey, String SecretAccessKey, String Message, String MobileNumber) {

    //Must be one of ("trace", "debug", "info", "warn", "error" or "off"). If not specified, defaults to "info".
    System.setProperty(org.slf4j.impl.SimpleLogger.DEFAULT_LOG_LEVEL_KEY, "info");
    
    AwsBasicCredentials    awsCreds = AwsBasicCredentials.create(AccessKey, SecretAccessKey);
    AwsCredentialsProvider awsCredProv = StaticCredentialsProvider.create(awsCreds);
        
        SnsClient sc = SnsClient.builder()
                        .region(Region.US_EAST_1)
                        .credentialsProvider(awsCredProv)
                        .build();
        
        String SMSMessage = Message;
        String mobile = MobileNumber;
        
        PublishRequest pubReq = PublishRequest.builder()
            .message(SMSMessage)
            .phoneNumber(mobile)
            .build();
        PublishResponse result = sc.publish(pubReq);

        System.out.println(result); // Prints the message ID.
}
#End If
 
Upvote 0

Bill Milleker

New Member
Licensed User
This can also be used with Build Standalone Package if you include this dependency:
B4X:
#AdditionalJar: url-connection-client-2.16.5.jar

and include this statement in the SendSms method:
B4X:
    System.setProperty("software.amazon.awssdk.http.service.impl", "software.amazon.awssdk.http.urlconnection.UrlConnectionSdkHttpService");
 
Last edited:
Upvote 0
Top