Added default IdGenerator and EventListener

This commit is contained in:
Vladimir Pankrashkin 2016-05-25 10:47:44 +03:00
parent 8212c13254
commit 9e50ccebd3
12 changed files with 50 additions and 30 deletions

View File

@ -12,9 +12,12 @@ import java.net.URI;
* Created by vpankrashkin on 25.04.16.
*/
public abstract class AbstractClientBuilder implements ClientBuilder {
private static final ClientEventListener DEFAULT_EVENT_LISTENER = (event) -> {
};
private URI address;
private ClientEventListener eventListener;
private IdGenerator idGenerator;
private ClientEventListener eventListener = DEFAULT_EVENT_LISTENER;
@Override
public ClientBuilder withAddress(URI address) {

View File

@ -12,8 +12,10 @@ import com.rbkmoney.woody.api.trace.context.MetadataTracer;
* Created by vpankrashkin on 10.05.16.
*/
public abstract class AbstractServiceBuilder<Service> implements ServiceBuilder<Service> {
private ServiceEventListener eventListener;
private static final ServiceEventListener DEFAULT_EVENT_LISTENER = (event) -> {
};
private ServiceEventListener eventListener = DEFAULT_EVENT_LISTENER;
@Override
public ServiceBuilder withEventListener(ServiceEventListener listener) {

View File

@ -0,0 +1,11 @@
package com.rbkmoney.woody.api.event;
/**
* Created by vpankrashkin on 25.05.16.
*/
public class EmptyEventListener implements EventListener {
@Override
public void notifyEvent(Event event) {
}
}

View File

@ -2,6 +2,7 @@ package com.rbkmoney.woody.thrift.impl.http;
import com.rbkmoney.woody.api.AbstractClientBuilder;
import com.rbkmoney.woody.api.event.ClientEventListener;
import com.rbkmoney.woody.api.generator.IdGenerator;
import com.rbkmoney.woody.api.interceptor.CommonInterceptor;
import com.rbkmoney.woody.api.interceptor.CompositeInterceptor;
import com.rbkmoney.woody.api.interceptor.ContainerCommonInterceptor;
@ -12,6 +13,7 @@ import com.rbkmoney.woody.api.trace.context.EmptyTracer;
import com.rbkmoney.woody.api.trace.context.TraceContext;
import com.rbkmoney.woody.api.transport.TransportEventInterceptor;
import com.rbkmoney.woody.thrift.impl.http.event.THClientEvent;
import com.rbkmoney.woody.thrift.impl.http.generator.TimestampIdGenerator;
import com.rbkmoney.woody.thrift.impl.http.interceptor.THCMessageRequestInterceptor;
import com.rbkmoney.woody.thrift.impl.http.interceptor.THCMessageResponseInterceptor;
import com.rbkmoney.woody.thrift.impl.http.interceptor.THCRequestInterceptor;
@ -19,7 +21,7 @@ import com.rbkmoney.woody.thrift.impl.http.interceptor.THCResponseInterceptor;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.thrift.TServiceClient;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.THttpClient;
import org.apache.thrift.transport.TTransport;
@ -33,7 +35,14 @@ import java.util.Optional;
* Created by vpankrashkin on 28.04.16.
*/
public class THClientBuilder extends AbstractClientBuilder {
private HttpClient httpClient = createHttpClient();
private static final IdGenerator DEFAULT_ID_GENERATOR = new TimestampIdGenerator();
private HttpClient httpClient;
public THClientBuilder() {
this.httpClient = createHttpClient();
super.withIdGenerator(DEFAULT_ID_GENERATOR);
}
public THClientBuilder withHttpClient(HttpClient httpClient) {
this.httpClient = httpClient;
@ -98,7 +107,7 @@ public class THClientBuilder extends AbstractClientBuilder {
}
protected TProtocol createProtocol(TTransport tTransport) {
return new TCompactProtocol(tTransport);
return new TBinaryProtocol(tTransport);
}
protected HttpClient createHttpClient() {

View File

@ -14,7 +14,7 @@ import com.rbkmoney.woody.api.transport.TransportEventInterceptor;
import com.rbkmoney.woody.thrift.impl.http.event.THServiceEvent;
import com.rbkmoney.woody.thrift.impl.http.interceptor.*;
import org.apache.thrift.TProcessor;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.protocol.TProtocolFactory;
import org.apache.thrift.server.TServlet;
@ -112,6 +112,10 @@ public class THServiceBuilder extends AbstractServiceBuilder<Servlet> {
}
protected TProtocolFactory createProtocolFactory() {
return new TBinaryProtocol.Factory();
}
protected TProtocolFactory wrapProtocolFactory(TProtocolFactory tProtocolFactory, CommonInterceptor commonInterceptor) {
return tTransport -> {
TProtocol tProtocol = tProtocolFactory.getProtocol(tTransport);
@ -125,7 +129,7 @@ public class THServiceBuilder extends AbstractServiceBuilder<Servlet> {
new ContainerCommonInterceptor(null, new THSResponseMetadataInterceptor(metadataExtender)),
new ContainerCommonInterceptor(null, new THSResponseInterceptor(false))
);
TProtocolFactory tProtocolFactory = wrapProtocolFactory(new TCompactProtocol.Factory(), protInterceptor);
TProtocolFactory tProtocolFactory = wrapProtocolFactory(createProtocolFactory(), protInterceptor);
return new TServlet(tProcessor, tProtocolFactory, servletInterceptor);
}

View File

@ -1,11 +1,11 @@
package com.rbkmoney.woody.thrift.impl.http;
package com.rbkmoney.woody.thrift.impl.http.generator;
import com.rbkmoney.woody.api.generator.IdGenerator;
/**
* Created by vpankrashkin on 06.05.16.
*/
public class IdGeneratorStub implements IdGenerator {
public class TimestampIdGenerator implements IdGenerator {
@Override
public String generateId(long timestamp) {
return Long.toString(timestamp);

View File

@ -18,10 +18,11 @@ public class THSResponseInterceptor implements ResponseInterceptor {
public static final Function<Object, String> THRFIT_TRANSPORT_ERROR_FUNC = obj -> "thrift transport error";
public static final Function<Object, String> THRFIT_PROTOCOL_ERROR_FUNC = obj -> "thrift protocol error";
public static final Function<Object, String> UNKNOWN_PROVIDER_ERROR_FUNC = obj -> "unknown provider error";
public static final Function<Object, String> UNKNOWN_CALL_FUNC = callName -> "Unknown method:" + callName;
public static final Function<String, String> BAD_CONTENT_TYPE_FUNC = cType -> "content type wrong/missing";
public static final Function<THttpHeader, String> BAD_REQUEST_HEADERS_FUNC = tHttpHeader -> (tHttpHeader == null ? "Trace header" : tHttpHeader.getKeyValue()) + " missing";
public static final Function<String, String> BAD_REQUEST_METHOD_FUNC = rewMethod -> "http method wrong";
boolean isUseContext;
private boolean isUseContext;
public THSResponseInterceptor(boolean isUseContext) {
this.isUseContext = isUseContext;
@ -76,7 +77,7 @@ public class THSResponseInterceptor implements ResponseInterceptor {
switch (tErrorType) {
case UNKNOWN_CALL:
responseStatus = 405;
errThriftValue = "Unknown method:" + metadata.getValue(MetadataProperties.CALL_NAME);
errThriftValue = UNKNOWN_CALL_FUNC.apply(metadata.getValue(MetadataProperties.CALL_NAME));
break;
case TRANSPORT:
TTransportErrorType tTransportErrorType = ContextUtils.getMetadataParameter(serviceSpan, TTransportErrorType.class, THMetadataProperties.TH_ERROR_SUBTYPE);

View File

@ -7,6 +7,7 @@ import com.rbkmoney.woody.rpc.Owner;
import com.rbkmoney.woody.rpc.OwnerService;
import com.rbkmoney.woody.rpc.test_error;
import com.rbkmoney.woody.thrift.impl.http.event.*;
import com.rbkmoney.woody.thrift.impl.http.generator.TimestampIdGenerator;
import org.apache.thrift.TException;
import org.apache.thrift.transport.TTransportException;
import org.junit.Assert;
@ -29,8 +30,8 @@ public class TestChildRequests extends AbstractTest {
new HttpClientEventLogListener()
);
OwnerService.Iface client1 = createThriftRPCClient(OwnerService.Iface.class, new IdGeneratorStub(), clientEventLogListener, getUrlString("/rpc"));
OwnerService.Iface client2 = createThriftRPCClient(OwnerService.Iface.class, new IdGeneratorStub(), clientEventLogListener, getUrlString("/rpc"));
OwnerService.Iface client1 = createThriftRPCClient(OwnerService.Iface.class, new TimestampIdGenerator(), clientEventLogListener, getUrlString("/rpc"));
OwnerService.Iface client2 = createThriftRPCClient(OwnerService.Iface.class, new TimestampIdGenerator(), clientEventLogListener, getUrlString("/rpc"));
OwnerService.Iface handler = new OwnerServiceStub() {
@Override
public Owner getErrOwner(int id) throws TException, test_error {

View File

@ -8,6 +8,7 @@ import com.rbkmoney.woody.rpc.Owner;
import com.rbkmoney.woody.rpc.OwnerService;
import com.rbkmoney.woody.rpc.test_error;
import com.rbkmoney.woody.thrift.impl.http.event.THClientEvent;
import com.rbkmoney.woody.thrift.impl.http.generator.TimestampIdGenerator;
import org.apache.thrift.TException;
import org.junit.Test;
@ -45,7 +46,7 @@ public class TestClientEventHandling extends AbstractTest {
public void testExpectedError() {
addServlet(createMutableTervlet(), "/");
OwnerService.Iface client = createThriftRPCClient(OwnerService.Iface.class, new IdGeneratorStub(), (ClientEventListener<THClientEvent>) (THClientEvent thClientEvent) -> {
OwnerService.Iface client = createThriftRPCClient(OwnerService.Iface.class, new TimestampIdGenerator(), (ClientEventListener<THClientEvent>) (THClientEvent thClientEvent) -> {
switch (thClientEvent.getEventType()) {
case CALL_SERVICE:
assertArrayEquals(new Object[]{0}, thClientEvent.getCallArguments());
@ -91,7 +92,7 @@ public class TestClientEventHandling extends AbstractTest {
public void testGetOwnerOK() {
addServlet(createMutableTervlet(), "/");
OwnerService.Iface client = createThriftRPCClient(OwnerService.Iface.class, new IdGeneratorStub(), (ClientEventListener<THClientEvent>) (THClientEvent thClientEvent) -> {
OwnerService.Iface client = createThriftRPCClient(OwnerService.Iface.class, new TimestampIdGenerator(), (ClientEventListener<THClientEvent>) (THClientEvent thClientEvent) -> {
switch (thClientEvent.getEventType()) {
case CALL_SERVICE:
assertArrayEquals(new Object[]{1}, thClientEvent.getCallArguments());
@ -132,7 +133,7 @@ public class TestClientEventHandling extends AbstractTest {
public void testUnexpectedError() {
addServlet(createMutableTervlet(), "/");
OwnerService.Iface client = createThriftRPCClient(OwnerService.Iface.class, new IdGeneratorStub(), (ClientEventListener<THClientEvent>) (THClientEvent thClientEvent) -> {
OwnerService.Iface client = createThriftRPCClient(OwnerService.Iface.class, new TimestampIdGenerator(), (ClientEventListener<THClientEvent>) (THClientEvent thClientEvent) -> {
switch (thClientEvent.getEventType()) {
case CALL_SERVICE:
assertArrayEquals(new Object[]{0}, thClientEvent.getCallArguments());

View File

@ -7,6 +7,7 @@ import com.rbkmoney.woody.thrift.impl.http.event.ClientActionListener;
import com.rbkmoney.woody.thrift.impl.http.event.ClientEventListenerImpl;
import com.rbkmoney.woody.thrift.impl.http.event.ServiceActionListener;
import com.rbkmoney.woody.thrift.impl.http.event.ServiceEventListenerImpl;
import com.rbkmoney.woody.thrift.impl.http.generator.TimestampIdGenerator;
import org.apache.thrift.TException;
import org.junit.Assert;
import org.junit.Before;
@ -37,7 +38,7 @@ public class TestEventOrder extends AbstractTest {
Servlet servlet = createThrftRPCService(OwnerService.Iface.class, handler, serviceEventListener);
OwnerService.Iface client = createThriftRPCClient(OwnerService.Iface.class, new IdGeneratorStub(), clientEventListener, getUrlString("/rpc"));
OwnerService.Iface client = createThriftRPCClient(OwnerService.Iface.class, new TimestampIdGenerator(), clientEventListener, getUrlString("/rpc"));
@Before
public void before() {

View File

@ -1,6 +1,5 @@
package com.rbkmoney.woody.thrift.impl.http;
import com.rbkmoney.woody.api.generator.IdGenerator;
import com.rbkmoney.woody.rpc.Owner;
import com.rbkmoney.woody.rpc.OwnerService;
import com.rbkmoney.woody.rpc.test_error;
@ -134,17 +133,6 @@ public class TestLoadErrThriftRPCClient {
THClientBuilder clientBuilder = new THClientBuilder();
clientBuilder.withAddress(new URI(url));
clientBuilder.withHttpClient(HttpClientBuilder.create().build());
clientBuilder.withIdGenerator(new IdGenerator() {
@Override
public String generateId(long timestamp) {
return Long.toString(timestamp);
}
@Override
public String generateId(long timestamp, int counter) {
return new StringBuilder().append(timestamp).append(':').append(counter).toString();
}
});
return clientBuilder.build(OwnerService.Iface.class);
}

View File

@ -137,7 +137,6 @@ public class TestLoadThriftRPCClient {
THClientBuilder clientBuilder = new THClientBuilder();
clientBuilder.withAddress(new URI(url));
clientBuilder.withHttpClient(HttpClientBuilder.create().build());
clientBuilder.withIdGenerator(new IdGeneratorStub());
clientBuilder.withEventListener(new ClientEventListenerImpl());
return clientBuilder.build(OwnerService.Iface.class);