Skip to content

Commit

Permalink
推送模块代码重构,支持不同的消息来源
Browse files Browse the repository at this point in the history
  • Loading branch information
夜色 committed Dec 24, 2016
1 parent e3cc21f commit 317c786
Show file tree
Hide file tree
Showing 31 changed files with 1,488 additions and 163 deletions.
Expand Up @@ -17,7 +17,7 @@
* ohun@live.cn (夜色)
*/

package com.mpush.common.condition;
package com.mpush.api.common;

import java.util.Map;
import java.util.function.Predicate;
Expand Down
53 changes: 53 additions & 0 deletions mpush-api/src/main/java/com/mpush/api/spi/push/IPushMessage.java
@@ -0,0 +1,53 @@
/*
* (C) Copyright 2015-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Contributors:
* ohun@live.cn (夜色)
*/

package com.mpush.api.spi.push;

import com.mpush.api.common.Condition;

/**
* Created by ohun on 2016/12/24.
*
* @author ohun@live.cn (夜色)
*/
public interface IPushMessage {

boolean isBroadcast();

String getUserId();

int getClientType();

byte[] getContent();

boolean isNeedAck();

byte getFlags();

int getTimeoutMills();

String getTaskId();

Condition getCondition();

default void finalized() {

}

}
29 changes: 29 additions & 0 deletions mpush-api/src/main/java/com/mpush/api/spi/push/MessagePusher.java
@@ -0,0 +1,29 @@
/*
* (C) Copyright 2015-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Contributors:
* ohun@live.cn (夜色)
*/

package com.mpush.api.spi.push;

/**
* Created by ohun on 2016/12/24.
*
* @author ohun@live.cn (夜色)
*/
public interface MessagePusher {
void push(IPushMessage message);
}
@@ -0,0 +1,35 @@
/*
* (C) Copyright 2015-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Contributors:
* ohun@live.cn (夜色)
*/

package com.mpush.api.spi.push;

import com.mpush.api.spi.Factory;
import com.mpush.api.spi.SpiLoader;

/**
* Created by ohun on 2016/12/24.
*
* @author ohun@live.cn (夜色)
*/
public interface MessagePusherFactory extends Factory<MessagePusher> {

static MessagePusher create() {
return SpiLoader.load(MessagePusherFactory.class).get();
}
}
54 changes: 54 additions & 0 deletions mpush-api/src/main/java/com/mpush/api/spi/push/PushListener.java
@@ -0,0 +1,54 @@
package com.mpush.api.spi.push;

public interface PushListener<T extends IPushMessage> {

/**
* 消息下发成功后回调
* 如果消息需要ACK则该方法不会被调用
*
* @param message 要下发的消息
*/
void onSuccess(T message);

/**
* 收到客户端ACK后回调
*
* @param message 要下发的消息
*/
void onAckSuccess(T message);

/**
* 广播消息推送全部结束后回调
*
* @param message 要下发的消息
*/
void onBroadcastComplete(T message);

/**
* 消息下发失败后回调
*
* @param message 要下发的消息
*/
void onFailure(T message);

/**
* 推送消息发现用户不在线时回调
*
* @param message 要下发的消息
*/
void onOffline(T message);

/**
* 推送消息发现用户不在当前机器时回调
*
* @param message 要下发的消息
*/
void onRedirect(T message);

/**
* 等待客户端ACK超时时回调
*
* @param message 要下发的消息
*/
void onAckTimeout(T message);
}
@@ -0,0 +1,36 @@
/*
* (C) Copyright 2015-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Contributors:
* ohun@live.cn (夜色)
*/

package com.mpush.api.spi.push;

import com.mpush.api.spi.Factory;
import com.mpush.api.spi.SpiLoader;

/**
* Created by ohun on 2016/12/24.
*
* @author ohun@live.cn (夜色)
*/
public interface PushListenerFactory<M extends IPushMessage> extends Factory<PushListener<M>> {

@SuppressWarnings("unchecked")
static <M extends IPushMessage> PushListener<M> create() {
return (PushListener<M>) SpiLoader.load(PushListenerFactory.class).get();
}
}
Expand Up @@ -19,6 +19,8 @@

package com.mpush.common.condition;

import com.mpush.api.common.Condition;

import java.util.Map;

/**
Expand Down
Expand Up @@ -19,6 +19,8 @@

package com.mpush.common.condition;

import com.mpush.api.common.Condition;

import javax.script.*;
import java.util.Map;

Expand Down
Expand Up @@ -19,8 +19,7 @@

package com.mpush.common.condition;

import com.mpush.tools.Jsons;
import com.mpush.tools.log.Logs;
import com.mpush.api.common.Condition;

import java.util.Map;
import java.util.Set;
Expand Down
Expand Up @@ -20,8 +20,10 @@
package com.mpush.common.message.gateway;

import com.google.gson.reflect.TypeToken;
import com.mpush.api.common.Condition;
import com.mpush.api.connection.Connection;
import com.mpush.api.protocol.Packet;
import com.mpush.api.spi.push.IPushMessage;
import com.mpush.common.condition.*;
import com.mpush.common.memory.PacketFactory;
import com.mpush.common.message.ByteBufMessage;
Expand All @@ -38,7 +40,7 @@
*
* @author ohun@live.cn
*/
public final class GatewayPushMessage extends ByteBufMessage {
public final class GatewayPushMessage extends ByteBufMessage implements IPushMessage {
public String userId;
public int clientType;
public int timeout;
Expand Down Expand Up @@ -127,24 +129,47 @@ public GatewayPushMessage setCondition(String condition) {
return this;
}

@Override
public boolean isBroadcast() {
return userId == null;
}

public boolean needAck() {
return packet.hasFlag(Packet.FLAG_BIZ_ACK) || packet.hasFlag(Packet.FLAG_AUTO_ACK);
@Override
public String getUserId() {
return userId;
}

@Override
public void send() {
super.sendRaw();
public int getClientType() {
return clientType;
}

@Override
public void send(ChannelFutureListener listener) {
super.sendRaw(listener);
public int getTimeoutMills() {
return timeout;
}

@Override
public String getTaskId() {
return taskId;
}

@Override
public byte[] getContent() {
return content;
}

@Override
public boolean isNeedAck() {
return packet.hasFlag(Packet.FLAG_BIZ_ACK) || packet.hasFlag(Packet.FLAG_AUTO_ACK);
}

@Override
public byte getFlags() {
return packet.flags;
}

@Override
public Condition getCondition() {
if (condition != null) {
return new ScriptCondition(condition);
Expand All @@ -155,6 +180,24 @@ public Condition getCondition() {
return AwaysPassCondition.I;
}


@Override
public void finalized() {
this.content = null;
this.condition = null;
this.tags = null;
}

@Override
public void send() {
super.sendRaw();
}

@Override
public void send(ChannelFutureListener listener) {
super.sendRaw(listener);
}

@Override
public String toString() {
return "GatewayPushMessage{" +
Expand Down

0 comments on commit 317c786

Please sign in to comment.