Skip to content

Commit

Permalink
代码优化
Browse files Browse the repository at this point in the history
  • Loading branch information
闫逍旭 committed Feb 18, 2016
1 parent 60b77d5 commit 4e05586
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 30 deletions.
49 changes: 20 additions & 29 deletions mpush-client/src/main/java/com/shinemo/mpush/push/PushRequest.java
Expand Up @@ -16,22 +16,24 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.atomic.AtomicInteger;

/**
* Created by ohun on 2015/12/30.
*/
public class PushRequest implements PushSender.Callback, Runnable {

private static GatewayServerManage gatewayClientManage = (GatewayServerManage)ServiceContainer.getInstance(ServerManage.class, "gatewayServerManage");

private static final Logger LOGGER = LoggerFactory.getLogger(PushRequest.class);

private final static GatewayServerManage gatewayClientManage = (GatewayServerManage) ServiceContainer.getInstance(ServerManage.class, "gatewayServerManage");

private PushSender.Callback callback;
private String userId;
private String content;
private long timeout;
private int status = 0;
private long timeout_;
private int sessionId;
private long sendTime;
private AtomicInteger status = new AtomicInteger(0);

public PushRequest() {
}
Expand Down Expand Up @@ -60,14 +62,6 @@ public PushRequest setTimeout(long timeout) {
return this;
}

public void setSessionId(int sessionId) {
this.sessionId = sessionId;
}

public int getSessionId() {
return sessionId;
}

@Override
public void onSuccess(String userId) {
submit(1);
Expand All @@ -89,20 +83,18 @@ public void onTimeout(String userId) {
}

private void submit(int status) {
if (this.status != 0) {//防止重复调用
return;
}
this.status = status;
if (callback != null) {
PushRequestBus.INSTANCE.getExecutor().execute(this);
} else {
LOGGER.warn("callback is null");
if (this.status.compareAndSet(0, status)) {//防止重复调用
if (callback != null) {
PushRequestBus.INSTANCE.getExecutor().execute(this);
} else {
LOGGER.warn("callback is null");
}
}
}

@Override
public void run() {
switch (status) {
switch (status.get()) {
case 1:
callback.onSuccess(userId);
break;
Expand Down Expand Up @@ -145,11 +137,11 @@ public void send() {
}

public void redirect() {
this.status = 0;
this.timeout_ = timeout + System.currentTimeMillis();
ConnectionRouterManager.INSTANCE.invalidateLocalCache(userId);
sendToConnServer();
LOGGER.warn("user route has changed, userId={}, content={}", userId, content);
if (status.get() == 0) {
send();
}
}

private void sendToConnServer() {
Expand Down Expand Up @@ -185,9 +177,8 @@ public void operationComplete(ChannelFuture future) throws Exception {
PushRequestBus.INSTANCE.put(sessionId, this);
}

public long getSendTime() {
return sendTime;
}


public long getSendTime() {
return sendTime;
}

}
@@ -1,5 +1,7 @@
package com.shinemo.mpush.push;

import io.netty.util.internal.chmv8.ConcurrentHashMapV8;

import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.*;
Expand All @@ -9,7 +11,7 @@
*/
public class PushRequestBus implements Runnable {
public static final PushRequestBus INSTANCE = new PushRequestBus();
private Map<Integer, PushRequest> requests = new ConcurrentHashMap<>();
private Map<Integer, PushRequest> requests = new ConcurrentHashMapV8<>(1024);
private Executor executor = Executors.newFixedThreadPool(5);//test
private ScheduledExecutorService scheduledExecutor = Executors.newSingleThreadScheduledExecutor();//test

Expand Down

0 comments on commit 4e05586

Please sign in to comment.