com.gw.common.utils.BaseApi Maven / Gradle / Ivy
The newest version!
package com.gw.common.utils;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.app.common.exception.CodeException;
import com.app.common.utils.JsonUtils;
import com.gateway.connector.tcp.client.IMessage;
import com.gateway.connector.tcp.client.IRspCallBack;
@Component
public class BaseApi {
private Logger logger = LoggerFactory.getLogger(BaseApi.class);
@Autowired
protected GwClientResource gwClientResource;
private String sid;
private Hashtable, com.gateway.connector.tcp.client.IMessage> htMessage = new Hashtable, com.gateway.connector.tcp.client.IMessage>();
private Hashtable, List> htMessageTopics = new Hashtable, List>();
public interface IMessage {
void onMessage(String topic, T t);
}
public void init(String serverKey) {
gwClientResource.getGwClientWrapper().init(serverKey);
}
public void addClientConnectListener(String serviceKey, IConnectionListener clientConnectListener) {
gwClientResource.getGwClientWrapper().addClientConnectListener(serviceKey, clientConnectListener);
}
public void requestAsync(String serverName, String topic, String content, final Class clazz,
final IMessage rsp) throws Exception {
IRspCallBack message1 = new IRspCallBack() {
@Override
public void onRspMessage(String content) {
if (rsp != null) {
T t = null;
if (StringUtils.isNoneBlank(content)) {
t = JsonUtils.Deserialize(content, clazz);
}
rsp.onMessage(topic, t);
}
}
};
gwClientResource.getGwClientWrapper().requestAsync(null, serverName, topic, content, com.gateway.connector.utils.Consts.WaitTime, message1);
}
public List requestSync(String sid, String serverName, String topic, String content, Class clazz, boolean isList,
boolean checkJsonFormat, boolean isDeserialize) throws CodeException {
try {
logger.info("BaseApi requestSync sid:{}, serverName:{}, topic:{}", sid, serverName, topic);
content = gwClientResource.getGwClientWrapper().requestSync(sid, serverName, topic, content, com.gateway.connector.utils.Consts.WaitTime);
} catch (CodeException e) {
throw e;
} catch (Exception e) {
throw new CodeException(Consts.NoKnowCode, e.getMessage());
}
String printMsg = content;
if (content.length() >= 2000) {
printMsg = content.substring(0, 2000) + "... too long";
}
logger.info("{}[{}]->response content:{}", serverName, topic, printMsg);
if (isDeserialize) {
if (!checkJsonFormat) {
try {
if (!isList) {
List lt = new ArrayList();
T t = JsonUtils.Deserialize(content, clazz);
lt.add(t);
return lt;
} else {
return JsonUtils.Deserialize2(content, clazz);
}
} catch (Exception e) {
throw new CodeException(Consts.NoKnowCode, e.getMessage());
}
} else {
throw new CodeException(Consts.NoKnowCode, content);
}
} else {
List lt = new ArrayList();
lt.add((T) content);
return lt;
}
}
@SuppressWarnings("unchecked")
public List requestSync(String serverName, String topic, String content, Class clazz, boolean isList,
boolean checkJsonFormat, boolean isDeserialize) throws CodeException {
try {
logger.info("requestSync no sid param, serveName:{}, topic:{}, content:{}", serverName, topic, content);
content = gwClientResource.getGwClientWrapper().requestSync(sid, serverName, topic, content, 30 * 1000);
} catch (CodeException e) {
throw e;
} catch (Exception e) {
throw new CodeException(Consts.NoKnowCode, e.getMessage());
}
String printMsg = content;
if (content.length() >= 2000) {
printMsg = content.substring(0, 2000) + "... too long";
}
logger.info("{}[{}]->response content:{}", serverName, topic, printMsg);
if (isDeserialize) {
if (!checkJsonFormat) {
try {
if (!isList) {
List lt = new ArrayList();
T t = JsonUtils.Deserialize(content, clazz);
lt.add(t);
return lt;
} else {
return JsonUtils.Deserialize2(content, clazz);
}
} catch (Exception e) {
throw new CodeException(Consts.NoKnowCode, e.getMessage());
}
} else {
throw new CodeException(Consts.NoKnowCode, content);
}
} else {
List lt = new ArrayList();
lt.add((T) content);
return lt;
}
}
public void subscribeWithImage(String serverName, String SvrId, String topic, final IMessage message,
final Class clazz, final boolean... LogDebug) {
com.gateway.connector.tcp.client.IMessage messageListener = new com.gateway.connector.tcp.client.IMessage() {
@Override
public void onMessage(String topic, String content) {
try {
if (StringUtils.isNoneBlank()) {
String printMsg = content;
if (content.length() >= 2000) {
printMsg = content.substring(0, 2000) + "... too long";
}
if (LogDebug != null && LogDebug.length > 0 && LogDebug[0] == true) {
logger.debug("topic:{},content:{}", topic, printMsg);
} else {
logger.info("topic:{},content:{}", topic, printMsg);
}
int index = content.indexOf("[");
if (index == 0) {
List t2 = JsonUtils.Deserialize2(content, clazz);
for (T t : t2) {
message.onMessage(topic, t);
}
} else {
T t = JsonUtils.Deserialize(content, clazz);
message.onMessage(topic, t);
}
}
} catch (Exception e) {
logger.error(topic, e);
}
}
};
List tempLt = htMessageTopics.get(message);
if (tempLt == null) {
tempLt = new ArrayList();
htMessageTopics.put(message, tempLt);
}
tempLt.add(topic);
htMessage.put(message, messageListener);
gwClientResource.getGwClientWrapper().subscribeWithImage(serverName, topic, SvrId, sid, messageListener);
}
public void subscribeWithImage(String serverKey, String topic, String svrID, String sid,
com.gateway.connector.tcp.client.IMessage messageListener) {
gwClientResource.getGwClientWrapper().subscribeWithImage(serverKey, topic, svrID, sid, messageListener);
}
}