Gradle 產生含 pom.xml 的 Jar 檔

build.gradle 加這些:

plugins {
    id 'maven-publish'
}

publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java
        }
    }
}

jar {
    into("META-INF/maven/$project.group/$project.name") {
        from { generatePomFileForMavenJavaPublication }
        rename ".*", "pom.xml"
    }
}

components.java 不是指 .java 檔,不用改它

然後跑 gradle publishToMavenLocal(我現在是只先做到產生 Jar 再自己上傳到 packagecloud)

其他細節參考 https://docs.gradle.org/current/userguide/publishing_maven.html

 

Spring Cloud AWS – Assume Role & Proxy configuration for STS & SQS

@Configuration
public class AppConfig {
    private static final String SESSION_NAME = "sample";

    @Value("${proxy.host:#{null}}")
    private String proxyHost;

    @Value("${proxy.port:0}")
    private int proxyPort;

    @Value("${cloud.aws.region.static}")
    private String region;

    @Bean
    @Primary
    public AWSCredentialsProvider awsCredentialsProvider(
            @Value("${cloud.aws.credentials.accessKey}") String accessKey,
            @Value("${cloud.aws.credentials.secretKey}") String secretKey,
            @Value("${cloud.aws.role}") String role) {

        AWSSecurityTokenService stsClient = AWSSecurityTokenServiceClientBuilder.standard()
                .withRegion(region)
                .withClientConfiguration(clientConfiguration())
                .withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey)))
                .build();

        return new STSAssumeRoleSessionCredentialsProvider
                .Builder(role, SESSION_NAME)
                .withStsClient(stsClient)
                .build();
    }

    @Bean
    public QueueMessageHandlerFactory queueMessageHandlerFactory() {
        QueueMessageHandlerFactory factory = new QueueMessageHandlerFactory();
        MappingJackson2MessageConverter messageConverter = new MappingJackson2MessageConverter();

        messageConverter.setStrictContentTypeMatch(false);
        factory.setArgumentResolvers(Collections.singletonList(new PayloadArgumentResolver(messageConverter)));
        return factory;
    }

    @Bean(name = "amazonSQS")
    public AmazonSQSAsync amazonSQSAsyncClient(AWSCredentialsProvider awsCredentialsProvider) {
        return AmazonSQSAsyncClientBuilder.standard()
                .withRegion(region)
                .withCredentials(awsCredentialsProvider)
                .withClientConfiguration(clientConfiguration())
                .build();
    }

    @Bean
    ClientConfiguration clientConfiguration() {
        ClientConfiguration clientConfiguration = new ClientConfiguration();

        if (!StringUtils.isEmpty(proxyHost)) {
            clientConfiguration.setProxyHost(proxyHost);
            clientConfiguration.setProxyPort(proxyPort);
        }

        return clientConfiguration;
    }
}

Azure Functions 的 Java 啟動表現頗慘

先看圖

簡直慘不忍睹,問了微軟,他們建議是用 App Service 方案開啟 Always On

看 App Service 方案定價,似乎就等於租了一個虛擬機

感覺和 AWS Lambda 落差滿大,AWS Lambda 可是真的按使用計價,而且沒有這樣的 overhead

另外 Azure Functions 的 C# 表現,看起來也不如 AWS Lambda 穩定

最常見的 Java 計算日期差方法是有問題的

最常見的 Java 計算日期差方法,就是取毫秒相減除以一天的毫秒數,但這方法其實有問題

之前在做日期相關的東西時,剛好看到這篇

Java – calculate the difference between two dates

他指出當2個日期包含日光節約時間的起始時,算出來的天數會少一天

他的範例程式用的是倫敦時區,不過 台灣也實施過日光節約時間

程式稍微改一下,就會發現台灣時區也有同樣問題

TimeZone.setDefault(TimeZone.getTimeZone("Asia/Taipei"));

Date d3 = new Date("03/31/1974 12:00:00");
Date d4 = new Date("04/01/1974 12:00:00");

結果:

133934400000
134017200000
82800000
Manual – Days between: 31-三月-1974 and 01-四月-1974 is: 0
Calendar – Days between: 31-三月-1974 and 01-四月-1974 is: 1

避免這個問題的方法

  1. Java 7 似乎會提供 JSR 310 裡面應該會有可計算日期差的 API
  2. 使用 Joda-Time library

使用 Joda-Time 範例如下:

DateTime d1 = new DateTime(1974, 3, 31, 12, 0, 0, 0);
DateTime d2 = new DateTime(1974, 4, 1, 12, 0, 0, 0);
Days d = Days.daysBetween(d1, d2);
System.out.println(d.getDays());