0%

使用腾迅云产品时,一般情况下,腾迅都会提供一个 SDK 供我们调用。

拿云直播里面的混流做为案例,讲解一下 SDK 的使用方法。通过官方的代码生成器,可以生成一个 Demo 做初步验证。

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
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;

import com.tencentcloudapi.live.v20180801.LiveClient;

import com.tencentcloudapi.live.v20180801.models.CreateCommonMixStreamRequest;
import com.tencentcloudapi.live.v20180801.models.CreateCommonMixStreamResponse;

public class CreateCommonMixStream {
public static void main(String [] args) {
try {
Credential cred = new Credential("SecretId", "SecretKey");

HttpProfile httpProfile = new HttpProfile();
httpProfile.setEndpoint("live.tencentcloudapi.com");

ClientProfile clientProfile = new ClientProfile();
clientProfile.setHttpProfile(httpProfile);

LiveClient client = new LiveClient(cred, "ap-shanghai", clientProfile);

// 混流参数
String params = "{}";
CreateCommonMixStreamRequest req = CreateCommonMixStreamRequest.fromJsonString(params, CreateCommonMixStreamRequest.class);

CreateCommonMixStreamResponse resp = client.CreateCommonMixStream(req);

System.out.println(CreateCommonMixStreamRequest.toJsonString(resp));
} catch (TencentCloudSDKException e) {
System.out.println(e.toString());
}

}

}

我们可以把一些配置信息提取到 yml 配置文件中,同时让 Spring IOC 容器来管理 LiveClient。

1
2
3
4
5
tencent-cloud:
secretId:
secretKey:
endpoint: live.tencentcloudapi.com
clientRegion: ap-shanghai
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
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.live.v20180801.LiveClient;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;



@Configuration
@ConfigurationProperties("tencent-cloud")
@Data
public class TencentCloudConfig {

private String secretId;
private String secretKey;
// 混流腾迅服务地址: live.tencentcloudapi.com
private String endpoint;
private String clientRegion;


@Bean
public LiveClient getLiveClient() {

Credential cred = new Credential(secretId, secretKey);

HttpProfile httpProfile = new HttpProfile();
httpProfile.setEndpoint(endpoint);

// 还可以设置代理
// httpProfile.setProxyHost(host);
// httpProfile.setProxyPort(port);

ClientProfile clientProfile = new ClientProfile();
clientProfile.setHttpProfile(httpProfile);

LiveClient client = new LiveClient(cred, clientRegion, clientProfile);
return client;
}

}

在 Service 的实现中,我们就可以直接注入 LiveClient 了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@Service
public class MixStreamServiceImpl implements MixStreamService {

@Autowired
private LiveClient liveClient;

@Override
public CreateCommonMixStreamResponse mixStream() {
CreateCommonMixStreamRequest mixStreamRequest = new CreateCommonMixStreamRequest();
// 设置混流参数
// mixStreamRequest.setXXX
CreateCommonMixStreamResponse response;
try {
response = liveClient.CreateCommonMixStream(mixStreamRequest);
} catch (TencentCloudSDKException e) {
e.printStackTrace();
}
return response;
}
}

多线程环境下,我们要如何测试自己写的业务代码是否是线程安全的?

可以用到 CountDownLatch 这个类,让所有线程都 await, 然后 countDown 以后,所有线程共同执行。

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
63
64
65
66
67
68
69
70
71
72
73
74
package gy.finolo.concurrent;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;


public class ConcurrentDemo {

// 线程数
private static final int WORKER_COUNT = 100;

// 竞争资源
private static int TOTAL = 100;

public static void main(String[] args) {

ExecutorService executorService = Executors.newCachedThreadPool();
CountDownLatch cdl = new CountDownLatch(1);

for (int i = 0; i < WORKER_COUNT; i++) {
Worker worker = new Worker(cdl);
executorService.execute(worker);
}

try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}

cdl.countDown();

try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
executorService.shutdown();
System.out.println("TOTAL should be ZERO, but it's : " + TOTAL);
}

static class Worker implements Runnable {

private CountDownLatch countDownLatch;

public Worker(CountDownLatch countDownLatch) {
this.countDownLatch = countDownLatch;
}

@Override
public void run() {
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}

this.executeTask();
}

// 需要并发处理的逻辑
private void executeTask() {
int a = TOTAL - 1;
try {
TimeUnit.MILLISECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
TOTAL = a;
}
}
}

使用安卓搜狗输入法小米版时,经常会误点到如图所示的广告。

一般会启动小米的一个应用。关掉应用再回到原来页面,要耗费好几秒钟时间,非常恶心。

我原以为通过下图所示方法,修改工具栏选项就可以解决。

其实是不行的,这个方法只能修改这个广告左边的那些按钮。

要去掉这个广告按钮,得按如下方法操作:

设置 -> 更多设置 -> 语言和输入法 -> 搜狗输入法小米版 -> 输入习惯 -> 节日活动提醒 -> 关闭开关,搞定。