Skip to content

Commit 0b336b2

Browse files
committed
ApiBoot Message Push 初版集成.
1 parent 53cbd56 commit 0b336b2

File tree

11 files changed

+827
-0
lines changed

11 files changed

+827
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright [2019] [恒宇少年 - 于起宇]
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
~
17+
-->
18+
19+
<project xmlns="http://maven.apache.org/POM/4.0.0"
20+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
21+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
22+
<parent>
23+
<artifactId>api-boot-plugins</artifactId>
24+
<groupId>org.minbox.framework</groupId>
25+
<version>2.0.5.RC1</version>
26+
</parent>
27+
<modelVersion>4.0.0</modelVersion>
28+
<properties>
29+
<main.basedir>${basedir}/../../..</main.basedir>
30+
</properties>
31+
<description>
32+
ApiBoot 消息推送组件集成,目前集成:
33+
1. 极光推送
34+
</description>
35+
<artifactId>api-boot-plugin-message-push</artifactId>
36+
<dependencies>
37+
<!--ApiBoot Plugin-->
38+
<dependency>
39+
<groupId>org.minbox.framework</groupId>
40+
<artifactId>api-boot-plugin</artifactId>
41+
</dependency>
42+
<!--JPush Client-->
43+
<dependency>
44+
<groupId>cn.jpush.api</groupId>
45+
<artifactId>jpush-client</artifactId>
46+
</dependency>
47+
<!--SpringBoot Aop-->
48+
<dependency>
49+
<groupId>org.springframework.boot</groupId>
50+
<artifactId>spring-boot-starter-aop</artifactId>
51+
</dependency>
52+
</dependencies>
53+
54+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright [2019] [恒宇少年 - 于起宇]
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package org.minbox.framework.api.boot.plugin.message.push;
19+
20+
import org.minbox.framework.api.boot.common.exception.ApiBootException;
21+
import org.minbox.framework.api.boot.plugin.message.push.model.MessagePushBody;
22+
import org.minbox.framework.api.boot.plugin.message.push.model.PushClientConfig;
23+
24+
/**
25+
* ApiBoot Message Push Service
26+
*
27+
* @author:恒宇少年 - 于起宇
28+
* <p>
29+
* DateTime:2019-04-20 15:40
30+
* Blog:http://blog.yuqiyu.com
31+
* WebSite:http://www.jianshu.com/u/092df3f77bca
32+
* Gitee:https://gitee.com/hengboy
33+
* GitHub:https://github.com/hengboy
34+
*/
35+
public interface ApiBootMessagePushService {
36+
/**
37+
* execute push message
38+
*
39+
* @param messagePushBody request body
40+
* @throws ApiBootException ApiBoot Exception
41+
*/
42+
void executePush(MessagePushBody messagePushBody) throws ApiBootException;
43+
44+
/**
45+
* get current thread push client name
46+
*
47+
* @return push client name
48+
* @throws ApiBootException ApiBoot Exception
49+
*/
50+
String getCurrentPushClientName() throws ApiBootException;
51+
52+
/**
53+
* get current thread push client
54+
*
55+
* @return push message client
56+
* @throws ApiBootException ApiBoot Exception
57+
*/
58+
PushClientConfig getCurrentPushClient() throws ApiBootException;
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright [2019] [恒宇少年 - 于起宇]
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package org.minbox.framework.api.boot.plugin.message.push.annotation;
19+
20+
import java.lang.annotation.*;
21+
22+
/**
23+
* ApiBoot Message Push Switch
24+
* Used to configure push client information
25+
*
26+
* @author:恒宇少年 - 于起宇
27+
* <p>
28+
* DateTime:2019-04-20 16:19
29+
* Blog:http://blog.yuqiyu.com
30+
* WebSite:http://www.jianshu.com/u/092df3f77bca
31+
* Gitee:https://gitee.com/hengboy
32+
* GitHub:https://github.com/hengboy
33+
*/
34+
@Target({ElementType.METHOD})
35+
@Retention(RetentionPolicy.RUNTIME)
36+
@Documented
37+
public @interface MessagePushSwitch {
38+
/**
39+
* jpush client name
40+
*
41+
* @return client name
42+
*/
43+
String value() default "default";
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright [2019] [恒宇少年 - 于起宇]
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package org.minbox.framework.api.boot.plugin.message.push.aop.advistor;
19+
20+
import org.aopalliance.aop.Advice;
21+
import org.minbox.framework.api.boot.plugin.message.push.annotation.MessagePushSwitch;
22+
import org.minbox.framework.api.boot.plugin.message.push.aop.interceptor.ApiBootMessagePushSwitchAnnotationInterceptor;
23+
import org.springframework.aop.Pointcut;
24+
import org.springframework.aop.support.AbstractPointcutAdvisor;
25+
import org.springframework.aop.support.annotation.AnnotationMatchingPointcut;
26+
import org.springframework.beans.BeansException;
27+
import org.springframework.beans.factory.BeanFactory;
28+
import org.springframework.beans.factory.BeanFactoryAware;
29+
import org.springframework.util.Assert;
30+
31+
/**
32+
* ApiBoot DataSource Switch
33+
*
34+
* @author:恒宇少年 - 于起宇
35+
* <p>
36+
* DateTime:2019-04-01 16:29
37+
* Blog:http://blog.yuqiyu.com
38+
* WebSite:http://www.jianshu.com/u/092df3f77bca
39+
* Gitee:https://gitee.com/hengboy
40+
* GitHub:https://github.com/hengboy
41+
*/
42+
public class ApiBootMessagePushClientSwitchAdvisor extends AbstractPointcutAdvisor implements BeanFactoryAware {
43+
44+
private Advice advice;
45+
private Pointcut pointcut;
46+
private BeanFactory beanFactory;
47+
48+
/**
49+
* init config
50+
*/
51+
public ApiBootMessagePushClientSwitchAdvisor(ApiBootMessagePushSwitchAnnotationInterceptor apiBootMessagePushSwitchAnnotationInterceptor) {
52+
// build pointcut instance
53+
this.pointcut = buildPointcut();
54+
// build advice instance
55+
this.advice = apiBootMessagePushSwitchAnnotationInterceptor;
56+
if (this.advice instanceof BeanFactoryAware) {
57+
((BeanFactoryAware) this.advice).setBeanFactory(beanFactory);
58+
}
59+
}
60+
61+
@Override
62+
public Pointcut getPointcut() {
63+
Assert.notNull(this.pointcut, "pointcut is required.");
64+
return this.pointcut;
65+
}
66+
67+
@Override
68+
public Advice getAdvice() {
69+
Assert.notNull(this.advice, "advice is required.");
70+
return this.advice;
71+
}
72+
73+
@Override
74+
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
75+
this.beanFactory = beanFactory;
76+
}
77+
78+
/**
79+
* build pointcut instance
80+
*/
81+
private Pointcut buildPointcut() {
82+
// method
83+
Pointcut mpc = AnnotationMatchingPointcut.forMethodAnnotation(MessagePushSwitch.class);
84+
return mpc;
85+
}
86+
87+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright [2019] [恒宇少年 - 于起宇]
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package org.minbox.framework.api.boot.plugin.message.push.aop.holder;
19+
20+
import org.springframework.util.Assert;
21+
22+
/**
23+
* Message Push Context Holer
24+
*
25+
* @author:恒宇少年 - 于起宇
26+
* <p>
27+
* DateTime:2019-04-20 16:33
28+
* Blog:http://blog.yuqiyu.com
29+
* WebSite:http://www.jianshu.com/u/092df3f77bca
30+
* Gitee:https://gitee.com/hengboy
31+
* GitHub:https://github.com/hengboy
32+
*/
33+
public class MessagePushContextHolder {
34+
/**
35+
* current thread data source pool name
36+
*/
37+
private static ThreadLocal<String> MESSAGE_PUSH_CLIENT_NAME = new ThreadLocal();
38+
39+
/**
40+
* setting current thread pool name
41+
*
42+
* @param dataSourcePoolName datasource pool name
43+
*/
44+
public static void set(String dataSourcePoolName) {
45+
Assert.notNull(dataSourcePoolName, "DataSource pool name is required.");
46+
MESSAGE_PUSH_CLIENT_NAME.set(dataSourcePoolName);
47+
}
48+
49+
/**
50+
* get current thread pool name
51+
*
52+
* @return data source pool name
53+
*/
54+
public static String get() {
55+
return MESSAGE_PUSH_CLIENT_NAME.get();
56+
}
57+
58+
/**
59+
* remove current thread pool name
60+
*/
61+
public static void remove() {
62+
MESSAGE_PUSH_CLIENT_NAME.remove();
63+
}
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright [2019] [恒宇少年 - 于起宇]
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package org.minbox.framework.api.boot.plugin.message.push.aop.interceptor;
19+
20+
import org.aopalliance.intercept.MethodInterceptor;
21+
import org.aopalliance.intercept.MethodInvocation;
22+
import org.minbox.framework.api.boot.plugin.message.push.annotation.MessagePushSwitch;
23+
import org.minbox.framework.api.boot.plugin.message.push.aop.holder.MessagePushContextHolder;
24+
import org.springframework.aop.support.AopUtils;
25+
import org.springframework.core.BridgeMethodResolver;
26+
import org.springframework.util.ClassUtils;
27+
28+
import java.lang.reflect.Method;
29+
30+
/**
31+
* ApiBoot DataSource Advice
32+
* use spring aop
33+
*
34+
* @author:恒宇少年 - 于起宇
35+
* <p>
36+
* DateTime:2019-04-01 16:44
37+
* Blog:http://blog.yuqiyu.com
38+
* WebSite:http://www.jianshu.com/u/092df3f77bca
39+
* Gitee:https://gitee.com/hengboy
40+
* GitHub:https://github.com/hengboy
41+
*/
42+
public class ApiBootMessagePushSwitchAnnotationInterceptor implements MethodInterceptor {
43+
44+
@Override
45+
public Object invoke(MethodInvocation invocation) throws Throwable {
46+
try {
47+
Class<?> targetClass = (invocation.getThis() != null ? AopUtils.getTargetClass(invocation.getThis()) : null);
48+
Method specificMethod = ClassUtils.getMostSpecificMethod(invocation.getMethod(), targetClass);
49+
Method declaredMethod = BridgeMethodResolver.findBridgedMethod(specificMethod);
50+
51+
MessagePushSwitch messagePushSwitch = declaredMethod.getDeclaredAnnotation(MessagePushSwitch.class);
52+
// set current thread message push config name
53+
MessagePushContextHolder.set(messagePushSwitch.value());
54+
return invocation.proceed();
55+
} finally {
56+
// remove current thread use message push config name
57+
MessagePushContextHolder.remove();
58+
}
59+
60+
}
61+
}

0 commit comments

Comments
 (0)