Gradle - 用IDEA生成可執行的jar檔

這邊以加入AmazionSQS的dependency為例

在使用gradle打包完jar檔後執行,發現會出現NoClassDefFoundError的錯誤訊息。

Error Log

rammus@Rammus-MacBook-Pro.local:~/workspace/HelloWorld/build$ java -jar libs/HelloWorld-1.0.jar
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: com/amazonaws/services/sqs/AmazonSQS
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: com.amazonaws.services.sqs.AmazonSQS
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 7 more

Read More

Amazon SQS Long Polling

目的

  • 消除多次的 request,避免 empty response。
  • 兩種情況會回傳 response:
    • connection time out 回傳空值。
    • 1 ~ n 個 Message (MaxNumberOfMessages)。

好處

  • 降低empty response可以減少費用。

注意事項

  • 為ReceiveMessage 的 WaitTimeSeconds 參數設置的值 (介於 1 到 20 之間) 優先於 Queue 的 ReceiveMessageWaitTimeSeconds 设置的任何值。
  • WaitTimeSeconds 超過 20 秒時可能沒有作用,建議設在 1 ~ 20 之間。

Reference

SES Java Snippet

檢查自己的Email Identities是否可以寄信

// Instantiate an Amazon SES client, which will make the service call with the supplied AWS credentials.
AmazonSimpleEmailServiceClient client = new AmazonSimpleEmailServiceClient(credentials);

List<String> res = client.listIdentities().getIdentities();
for (String s : res) {
System.out.println(s);
}

Read More