use of com.weibo.api.motan.exception.MotanServiceException in project motan by weibocom.
the class RefererInvocationHandlerTest method testInvokeException.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testInvokeException() throws Throwable {
final Cluster cluster = mockery.mock(Cluster.class);
final URL u = new URL("motan", "local", 80, "test");
u.addParameter(URLParamType.nodeType.getName(), MotanConstants.NODE_TYPE_REFERER);
mockery.checking(new Expectations() {
{
one(cluster).call(with(any(Request.class)));
will(throwException(new MotanBizException("just test", new StackOverflowError())));
allowing(cluster).getUrl();
will(returnValue(u));
}
});
List<Cluster> clus = new ArrayList<Cluster>();
clus.add(cluster);
RefererInvocationHandler handler = new RefererInvocationHandler(String.class, clus);
Method[] methods = String.class.getMethods();
try {
handler.invoke(null, methods[1], null);
} catch (Exception e) {
assertTrue(e instanceof MotanServiceException);
assertTrue(e.getMessage().contains("StackOverflowError"));
}
}
use of com.weibo.api.motan.exception.MotanServiceException in project motan by weibocom.
the class AbstractInterfaceConfig method getLocalHostAddress.
protected String getLocalHostAddress(List<URL> registryURLs) {
String localAddress = null;
Map<String, Integer> regHostPorts = new HashMap<String, Integer>();
for (URL ru : registryURLs) {
if (StringUtils.isNotBlank(ru.getHost()) && ru.getPort() > 0) {
regHostPorts.put(ru.getHost(), ru.getPort());
}
}
InetAddress address = NetUtils.getLocalAddress(regHostPorts);
if (address != null) {
localAddress = address.getHostAddress();
}
if (NetUtils.isValidLocalHost(localAddress)) {
return localAddress;
}
throw new MotanServiceException("Please config local server hostname with intranet IP first!", MotanErrorMsgConstant.FRAMEWORK_INIT_ERROR);
}
use of com.weibo.api.motan.exception.MotanServiceException in project motan by weibocom.
the class ConfigUtil method parseExport.
/**
* export fomart: protocol1:port1,protocol2:port2
*
* @param export
* @return
*/
@SuppressWarnings("unchecked")
public static Map<String, Integer> parseExport(String export) {
if (StringUtils.isBlank(export)) {
return Collections.emptyMap();
}
Map<String, Integer> pps = new HashMap<String, Integer>();
String[] protocolAndPorts = MotanConstants.COMMA_SPLIT_PATTERN.split(export);
for (String pp : protocolAndPorts) {
if (StringUtils.isBlank(pp)) {
continue;
}
String[] ppDetail = pp.split(":");
if (ppDetail.length == 2) {
pps.put(ppDetail[0], Integer.parseInt(ppDetail[1]));
} else if (ppDetail.length == 1) {
if (MotanConstants.PROTOCOL_INJVM.equals(ppDetail[0])) {
pps.put(ppDetail[0], MotanConstants.DEFAULT_INT_VALUE);
} else {
int port = MathUtil.parseInt(ppDetail[0], 0);
if (port <= 0) {
throw new MotanServiceException("Export is malformed :" + export);
} else {
pps.put(MotanConstants.PROTOCOL_MOTAN, port);
}
}
} else {
throw new MotanServiceException("Export is malformed :" + export);
}
}
return pps;
}
use of com.weibo.api.motan.exception.MotanServiceException in project motan by weibocom.
the class ActiveLimitFilter method filter.
@Override
public Response filter(Caller<?> caller, Request request) {
int maxAcvitivyCount = caller.getUrl().getIntParameter(URLParamType.actives.getName(), URLParamType.actives.getIntValue());
if (maxAcvitivyCount > 0) {
int activeCount = RpcStats.getServiceStat(caller.getUrl()).getActiveCount();
if (activeCount >= maxAcvitivyCount) {
throw new MotanServiceException(String.format("Request(%s) active count exceed the limit (%s), referer:%s", request, maxAcvitivyCount, caller.getUrl()), MotanErrorMsgConstant.SERVICE_REJECT);
}
}
long startTime = System.currentTimeMillis();
RpcStats.beforeCall(caller.getUrl(), request);
try {
Response rs = caller.call(request);
RpcStats.afterCall(caller.getUrl(), request, true, System.currentTimeMillis() - startTime);
return rs;
} catch (RuntimeException re) {
RpcStats.afterCall(caller.getUrl(), request, false, System.currentTimeMillis() - startTime);
throw re;
}
}
use of com.weibo.api.motan.exception.MotanServiceException in project motan by weibocom.
the class NettyChannel method request.
@Override
public Response request(Request request) throws TransportException {
int timeout = nettyClient.getUrl().getMethodParameter(request.getMethodName(), request.getParamtersDesc(), URLParamType.requestTimeout.getName(), URLParamType.requestTimeout.getIntValue());
if (timeout <= 0) {
throw new MotanFrameworkException("NettyClient init Error: timeout(" + timeout + ") <= 0 is forbid.", MotanErrorMsgConstant.FRAMEWORK_INIT_ERROR);
}
NettyResponseFuture response = new NettyResponseFuture(request, timeout, this.nettyClient);
this.nettyClient.registerCallback(request.getRequestId(), response);
ChannelFuture writeFuture = this.channel.write(request);
boolean result = writeFuture.awaitUninterruptibly(timeout, TimeUnit.MILLISECONDS);
if (result && writeFuture.isSuccess()) {
response.addListener(new FutureListener() {
@Override
public void operationComplete(Future future) throws Exception {
if (future.isSuccess() || (future.isDone() && ExceptionUtil.isBizException(future.getException()))) {
// 成功的调用
nettyClient.resetErrorCount();
} else {
// 失败的调用
nettyClient.incrErrorCount();
}
}
});
return response;
}
writeFuture.cancel();
response = this.nettyClient.removeCallback(request.getRequestId());
if (response != null) {
response.cancel();
}
// 失败的调用
nettyClient.incrErrorCount();
if (writeFuture.getCause() != null) {
throw new MotanServiceException("NettyChannel send request to server Error: url=" + nettyClient.getUrl().getUri() + " local=" + localAddress + " " + MotanFrameworkUtil.toString(request), writeFuture.getCause());
} else {
throw new MotanServiceException("NettyChannel send request to server Timeout: url=" + nettyClient.getUrl().getUri() + " local=" + localAddress + " " + MotanFrameworkUtil.toString(request));
}
}
Aggregations