上传本地jar包到Nexus

一些第三方提供的SDK,往往在Maven中央仓库是下载不到的,这个时候如果搭建了私服,就可以往上面丢

相关命令如下:

1
mvn deploy:deploy-file -DgroupId=org.budo -DartifactId=mipush_sdk_server -Dversion=2_2_21 -Dpackaging=jar -Dfile=MiPush_SDK_Server_2_2_21.jar -Durl=http://nexus-server:8081/nexus/content/repositories/thirdparty/ -DrepositoryId=snapshots.nexus.ewei.ren

settings.xml 关键配置如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<servers>
<server>
<id>snapshots.nexus.ewei.ren</id>
<username>deployment</username>
<password>12369.</password>
</server>
</servers>

<mirrors>
<mirror>
<id>snapshots.nexus.ewei.ren</id>
<name>nexus-ewei</name>
<url>http://nexus-server:8081/nexus/content/groups/public</url>
<mirrorOf>*</mirrorOf>
</mirror>
</mirrors>

关闭macOS所有过渡动画

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# opening and closing windows and popovers
defaults write -g NSAutomaticWindowAnimationsEnabled -bool false

# smooth scrolling
defaults write -g NSScrollAnimationEnabled -bool false

# showing and hiding sheets, resizing preference windows, zooming windows
# float 0 doesn't work
defaults write -g NSWindowResizeTime -float 0.001

# opening and closing Quick Look windows
defaults write -g QLPanelAnimationDuration -float 0

# rubberband scrolling (doesn't affect web views)
defaults write -g NSScrollViewRubberbanding -bool false

# resizing windows before and after showing the version browser
# also disabled by NSWindowResizeTime -float 0.001
defaults write -g NSDocumentRevisionsWindowTransformAnimation -bool false

# showing a toolbar or menu bar in full screen
defaults write -g NSToolbarFullScreenAnimationDuration -float 0

# scrolling column views
defaults write -g NSBrowserColumnAnimationSpeedMultiplier -float 0

# showing the Dock
defaults write com.apple.dock autohide-time-modifier -float 0
defaults write com.apple.dock autohide-delay -float 0

# showing and hiding Mission Control, command+numbers
defaults write com.apple.dock expose-animation-duration -float 0

# showing and hiding Launchpad
defaults write com.apple.dock springboard-show-duration -float 0
defaults write com.apple.dock springboard-hide-duration -float 0

# changing pages in Launchpad
defaults write com.apple.dock springboard-page-duration -float 0

# at least AnimateInfoPanes
defaults write com.apple.finder DisableAllAnimations -bool true

# sending messages and opening windows for replies
defaults write com.apple.Mail DisableSendAnimations -bool true
defaults write com.apple.Mail DisableReplyAnimations -bool true

简单轮询算法实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
fun main(args: Array<String>) {
val list = arrayListOf(0, 1, 2)

var prevIndex = -1
for (value in (1..7)) { // 执行7次测试
val currentIndex = (prevIndex + 1) % list.size

print("${list[currentIndex]} \t")

prevIndex = currentIndex
}
}

// 0 1 2 0 1 2 0
// TODO 加权

RestTemplate 跳过SSL证书验证

在使用RestTemplate请求接口的过程中,遇到HTTPS请求又没有证书的情况,只能通过配置来忽略证书验证了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.ewei.custom.yto.config

import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.http.client.ClientHttpRequestFactory
import org.springframework.http.client.SimpleClientHttpRequestFactory
import org.springframework.web.client.RestTemplate
import java.net.HttpURLConnection
import java.security.SecureRandom
import java.security.cert.X509Certificate
import javax.net.ssl.HttpsURLConnection
import javax.net.ssl.SSLContext
import javax.net.ssl.SSLSocketFactory
import javax.net.ssl.X509TrustManager


/**
* @author wuwenze
* @date 2019-06-21
*/
@Configuration
class RestTemplateClientConfig {

@Bean
fun restTemplate(factory: ClientHttpRequestFactory): RestTemplate {
return RestTemplate(factory)
}

@Bean
fun simpleClientHttpRequestFactory(): ClientHttpRequestFactory {
val factory = SkipSSLSimpleClientHttpRequestFactory()
factory.setReadTimeout(30000)
factory.setConnectTimeout(30000)
return factory
}

class SkipSSLSimpleClientHttpRequestFactory : SimpleClientHttpRequestFactory() {
override fun prepareConnection(connection: HttpURLConnection, httpMethod: String) {
if (connection is HttpsURLConnection) {
try {
connection.setHostnameVerifier { _, _ -> true }
connection.sslSocketFactory = createSslSocketFactory()
} catch (e: Throwable) {
// ignore
}
}
super.prepareConnection(connection, httpMethod)
}

private fun createSslSocketFactory(): SSLSocketFactory {
val context: SSLContext = SSLContext.getInstance("TLS")
context.init(null, arrayOf(SkipX509TrustManager()), SecureRandom())
return context.socketFactory
}

class SkipX509TrustManager : X509TrustManager {
override fun getAcceptedIssuers(): Array<X509Certificate> = arrayOf()
override fun checkClientTrusted(chain: Array<out X509Certificate>?, authType: String?) {}
override fun checkServerTrusted(chain: Array<out X509Certificate>?, authType: String?) {}
}
}
}

Kotlin 单例模式详解

单例模式很熟悉了,在Java中有各种创建姿势,算是比较麻烦的,这里就不再赘述了。

Object

那么如何在kotlin中实现单例模式呢?请看代码

1
2
object JsonObjectMapper {
}

仅需简单的把class关键字替换为object就完成了!

1
2
3
4
5
// Kotlin 调用
JsonObjectMapper

// Java调用
JsonObjectMapper.INSTANCE

究竟Kotlin封装了什么细节?让我们一探究竟吧:
通过Tools > Kotlin > Show Kotlin Bytecode 显示字节码,看看翻译后的Java代码
image.png

image.png

翻译成Java后,发现就是一个最简单的饿汉式单例而已。

Companion Object

懒汉式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class SingletonDemo private constructor() {
companion object {
private var instance: SingletonDemo? = null
get() {
if (field == null) {
field = SingletonDemo()
}
return field
}
fun get(): SingletonDemo{
return instance!!
}
}
}

线程安全的懒汉式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class SingletonDemo private constructor() {
companion object {
private var instance: SingletonDemo? = null
get() {
if (field == null) {
field = SingletonDemo()
}
return field
}
@Synchronized
fun get(): SingletonDemo{
return instance!!
}
}
}

双重校验锁式(Double Check)

1
2
3
4
5
6
class SingletonDemo private constructor() {
companion object {
val instance: SingletonDemo by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
SingletonDemo() }
}
}

静态内部类式

1
2
3
4
5
6
7
8
class SingletonDemo private constructor() {
companion object {
val instance = SingletonHolder.holder
}
private object SingletonHolder {
val holder = SingletonDemo()
}
}
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×