本文整理汇总了Java中com.twitter.hbc.core.event.EventType类的典型用法代码示例。如果您正苦于以下问题:Java EventType类的具体用法?Java EventType怎么用?Java EventType使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
EventType类属于com.twitter.hbc.core.event包,在下文中一共展示了EventType类的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: should_log_reason_that_client_stopped
import com.twitter.hbc.core.event.EventType; //导入依赖的package包/类
@Test
public void should_log_reason_that_client_stopped() {
Event event = new Event(EventType.STOPPED_BY_ERROR, new Exception("EXCEPTION"));
when(this.client.getExitEvent()).thenReturn(event);
HosebirdReader tr = new ConcreteHosebirdReader();
reset(this.logger);
tr.setLogger(this.logger);
tr.logClientExitReason(this.client);
verify(this.logger).error(
"Hosebird client stopped: {} {}",
new Object[]{
"STOPPED_BY_ERROR",
"EXCEPTION"
});
}
示例2: testInterruptedExceptionDuringProcessing
import com.twitter.hbc.core.event.EventType; //导入依赖的package包/类
@Test
public void testInterruptedExceptionDuringProcessing() throws Exception {
ClientBase clientBase = new ClientBase("name",
mockClient, new HttpHosts("http://hi"), new RawEndpoint("/endpoint", HttpConstants.HTTP_GET), mockAuth,
mockProcessor, mockReconnectionManager, mockRateTracker
);
when(mockStatusLine.getStatusCode())
.thenReturn(200);
doThrow(new InterruptedException()).when(mockProcessor).process();
when(mockClient.getConnectionManager())
.thenReturn(mockConnectionManager);
BasicClient client = new BasicClient(clientBase, executorService);
assertFalse(clientBase.isDone());
client.connect();
assertTrue(client.waitForFinish(100));
assertTrue(client.isDone());
verify(mockProcessor).setup(any(InputStream.class));
verify(mockConnectionManager, atLeastOnce()).shutdown();
assertEquals(EventType.STOPPED_BY_ERROR, client.getExitEvent().getEventType());
assertTrue(client.getExitEvent().getUnderlyingException() instanceof InterruptedException);
}
示例3: stop
import com.twitter.hbc.core.event.EventType; //导入依赖的package包/类
/**
* Stops the current connection. No reconnecting will occur. Kills thread + cleanup.
* Waits for the loop to end
**/
public void stop(int waitMillis) throws InterruptedException {
try {
if (!isDone()) {
setExitStatus(new Event(EventType.STOPPED_BY_USER, String.format("Stopped by user: waiting for %d ms", waitMillis)));
}
if (!waitForFinish(waitMillis)) {
logger.warn("{} Client thread failed to finish in {} millis", name, waitMillis);
}
} finally {
rateTracker.shutdown();
}
}
示例4: stop
import com.twitter.hbc.core.event.EventType; //导入依赖的package包/类
/**
* Stops the current connection. No reconnecting will occur. Kills thread + cleanup.
* Waits for the loop to end
**/
public void stop(int waitMillis) throws InterruptedException {
try {
if (!isDone()) {
setExitStatus(new Event(EventType.STOPPED_BY_USER, String.format("Stopped by user: waiting for %d ms", waitMillis)));
}
if (!waitForFinish(waitMillis)) {
logger.warn("{} Client thread failed to finish in {} millis", name, waitMillis);
}
} finally {
rateTracker.shutdown();
}
}
示例5: testIOExceptionDuringProcessing
import com.twitter.hbc.core.event.EventType; //导入依赖的package包/类
@Test
public void testIOExceptionDuringProcessing() throws Exception {
ClientBase clientBase = new ClientBase("name",
mockClient, new HttpHosts("http://hi"), new RawEndpoint("/endpoint", HttpConstants.HTTP_GET), mockAuth,
mockProcessor, mockReconnectionManager, mockRateTracker
);
BasicClient client = new BasicClient(clientBase, executorService);
final CountDownLatch latch = new CountDownLatch(1);
when(mockStatusLine.getStatusCode())
.thenReturn(200);
doNothing().when(mockProcessor).setup(any(InputStream.class));
doThrow(new IOException()).
doThrow(new IOException()).
doThrow(new IOException()).
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
latch.countDown();
return null;
}
}).when(mockProcessor).process();
client.connect();
latch.await();
assertFalse(clientBase.isDone());
verify(mockProcessor, times(4)).setup(any(InputStream.class));
// throw 3 exceptions, 4th one keeps going
verify(mockProcessor, atLeast(4)).process();
client.stop();
verify(mockConnectionManager, atLeastOnce()).shutdown();
assertTrue(client.isDone());
assertEquals(EventType.STOPPED_BY_USER, clientBase.getExitEvent().getEventType());
}
示例6: run
import com.twitter.hbc.core.event.EventType; //导入依赖的package包/类
@Override
public void run() {
// establish the initial connection
// if connection fails due to auth or some other 400, stop immediately
// if connection fails due to a 500, back off and retry
// if no response or other code, stop immediately
// begin reading from the stream
// while the stop signal hasn't been sent, and no IOException from processor, keep processing
// if IOException, time to restart the connection:
// handle http connection cleanup
// do some backoff, set backfill
// if stop signal set, time to kill/clean the connection, and end this thread.
try {
if (client instanceof RestartableHttpClient) {
((RestartableHttpClient) client).setup();
}
rateTracker.start();
while (!isDone()) {
String host = hosts.nextHost();
if (host == null) {
setExitStatus(new Event(EventType.STOPPED_BY_ERROR, "No hosts available"));
break;
}
double rate = rateTracker.getCurrentRateSeconds();
if (!Double.isNaN(rate)) {
endpoint.setBackfillCount(reconnectionManager.estimateBackfill(rate));
}
//logger.info("++++++++++++++++++++++++++++++++++++++ ");
//logger.info("Going to build a request ");
//logger.info("host : " +host);
//logger.info("endpointURI: " + endpoint.getURI());
//logger.info("endpointHTTPMethod : " + endpoint.getHttpMethod());
//logger.info("auth:" + auth);
//logger.info("auth:" + auth.toString());
HttpUriRequest request = HttpConstants.constructRequest(host, endpoint, auth);
if (request != null) {
String postContent = null;
if (endpoint.getHttpMethod().equalsIgnoreCase(HttpConstants.HTTP_POST)) {
postContent = endpoint.getPostParamString();
}
auth.signRequest(request, postContent);
Connection conn = new Connection(client, processor);
StatusLine status = establishConnection(conn, request);
if (handleConnectionResult(status)) {
rateTracker.resume();
processConnectionData(conn);
rateTracker.pause();
}
logger.info("{} Done processing, preparing to close connection", name);
conn.close();
} else {
addEvent(
new Event(
EventType.CONNECTION_ERROR,
String.format("Error creating request: %s, %s, %s", endpoint.getHttpMethod(), host, endpoint.getURI())
)
);
}
}
} catch (Throwable e) {
logger.warn(name + " Uncaught exception", e);
Exception laundered = (e instanceof Exception) ? (Exception) e : new RuntimeException(e);
setExitStatus(new Event(EventType.STOPPED_BY_ERROR, laundered));
} finally {
rateTracker.stop();
logger.info("{} Shutting down httpclient connection manager", name);
client.getConnectionManager().shutdown();
isRunning.countDown();
}
}
示例7: handleConnectionResult
import com.twitter.hbc.core.event.EventType; //导入依赖的package包/类
/**
* @return whether a successful connection has been established
*/
@VisibleForTesting
boolean handleConnectionResult(@Nullable StatusLine statusLine) {
statsReporter.incrNumConnects();
if (statusLine == null) {
logger.warn("{} failed to establish connection properly", name);
addEvent(new Event(EventType.CONNECTION_ERROR, "Failed to establish connection properly"));
return false;
}
int statusCode = statusLine.getStatusCode();
if (statusCode == HttpConstants.Codes.SUCCESS) {
logger.debug("{} Connection successfully established", name);
statsReporter.incrNum200s();
connectionEstablished.set(true);
addEvent(new HttpResponseEvent(EventType.CONNECTED, statusLine));
reconnectionManager.resetCounts();
return true;
}
logger.warn(name + " Error connecting w/ status code - {}, reason - {}", statusCode, statusLine.getReasonPhrase());
statsReporter.incrNumConnectionFailures();
addEvent(new HttpResponseEvent(EventType.HTTP_ERROR, statusLine));
if (HttpConstants.FATAL_CODES.contains(statusCode)) {
setExitStatus(new Event(EventType.STOPPED_BY_ERROR, "Fatal error code: " + statusCode));
} else if (statusCode < 500 && statusCode >= 400) {
statsReporter.incrNum400s();
// we will retry these a set number of times, then fail
if (reconnectionManager.shouldReconnectOn400s()) {
logger.debug("{} Reconnecting on {}", name, statusCode);
reconnectionManager.handleExponentialBackoff();
} else {
logger.debug("{} Reconnecting retries exhausted for {}", name, statusCode);
setExitStatus(new Event(EventType.STOPPED_BY_ERROR, "Retries exhausted"));
}
} else if (statusCode >= 500) {
statsReporter.incrNum500s();
reconnectionManager.handleExponentialBackoff();
} else {
setExitStatus(new Event(EventType.STOPPED_BY_ERROR, statusLine.getReasonPhrase()));
}
return false;
}
示例8: run
import com.twitter.hbc.core.event.EventType; //导入依赖的package包/类
@Override
public void run() {
// establish the initial connection
// if connection fails due to auth or some other 400, stop immediately
// if connection fails due to a 500, back off and retry
// if no response or other code, stop immediately
// begin reading from the stream
// while the stop signal hasn't been sent, and no IOException from processor, keep processing
// if IOException, time to restart the connection:
// handle http connection cleanup
// do some backoff, set backfill
// if stop signal set, time to kill/clean the connection, and end this thread.
try {
if (client instanceof RestartableHttpClient) {
((RestartableHttpClient) client).setup();
}
rateTracker.start();
while (!isDone()) {
String host = hosts.nextHost();
if (host == null) {
setExitStatus(new Event(EventType.STOPPED_BY_ERROR, "No hosts available"));
break;
}
double rate = rateTracker.getCurrentRateSeconds();
if (!Double.isNaN(rate)) {
endpoint.setBackfillCount(reconnectionManager.estimateBackfill(rate));
}
HttpUriRequest request = HttpConstants.constructRequest(host, endpoint, auth);
if (request != null) {
String postContent = null;
if (endpoint.getHttpMethod().equalsIgnoreCase(HttpConstants.HTTP_POST)) {
postContent = endpoint.getPostParamString();
}
auth.signRequest(request, postContent);
Connection conn = new Connection(client, processor);
StatusLine status = establishConnection(conn, request);
if (handleConnectionResult(status)) {
rateTracker.resume();
processConnectionData(conn);
rateTracker.pause();
}
logger.info("{} Done processing, preparing to close connection", name);
conn.close();
} else {
addEvent(
new Event(
EventType.CONNECTION_ERROR,
String.format("Error creating request: %s, %s, %s", endpoint.getHttpMethod(), host, endpoint.getURI())
)
);
}
}
} catch (Throwable e) {
logger.warn(name + " Uncaught exception", e);
Exception laundered = (e instanceof Exception) ? (Exception) e : new RuntimeException(e);
setExitStatus(new Event(EventType.STOPPED_BY_ERROR, laundered));
} finally {
rateTracker.stop();
logger.info("{} Shutting down httpclient connection manager", name);
client.getConnectionManager().shutdown();
isRunning.countDown();
}
}
示例9: handleConnectionResult
import com.twitter.hbc.core.event.EventType; //导入依赖的package包/类
/**
* @return whether a successful connection has been established
*/
@VisibleForTesting
boolean handleConnectionResult(@Nullable StatusLine statusLine) {
statsReporter.incrNumConnects();
if (statusLine == null) {
logger.warn("{} failed to establish connection properly", name);
addEvent(new Event(EventType.CONNECTION_ERROR, "Failed to establish connection properly"));
return false;
}
int statusCode = statusLine.getStatusCode();
if (statusCode == HttpConstants.Codes.SUCCESS) {
logger.debug("{} Connection successfully established", name);
statsReporter.incrNum200s();
connectionEstablished.set(true);
addEvent(new HttpResponseEvent(EventType.CONNECTED, statusLine));
reconnectionManager.resetCounts();
return true;
}
logger.warn(name + " Error connecting w/ status code - {}, reason - {}", statusCode, statusLine.getReasonPhrase());
statsReporter.incrNumConnectionFailures();
addEvent(new HttpResponseEvent(EventType.HTTP_ERROR, statusLine));
if (HttpConstants.FATAL_CODES.contains(statusCode)) {
setExitStatus(new Event(EventType.STOPPED_BY_ERROR, "Fatal error code: " + statusCode));
} else if (statusCode < 500 && statusCode >= 400) {
statsReporter.incrNum400s();
// we will retry these a set number of times, then fail
if (reconnectionManager.shouldReconnectOn400s()) {
logger.debug("{} Reconnecting on {}", name, statusCode);
reconnectionManager.handleExponentialBackoff();
} else {
logger.debug("{} Reconnecting retries exhausted for {}", name, statusCode);
setExitStatus(new Event(EventType.STOPPED_BY_ERROR, "Retries exhausted"));
}
} else if (statusCode >= 500) {
statsReporter.incrNum500s();
reconnectionManager.handleExponentialBackoff();
} else {
setExitStatus(new Event(EventType.STOPPED_BY_ERROR, statusLine.getReasonPhrase()));
}
return false;
}
示例10: testConnectionRetries
import com.twitter.hbc.core.event.EventType; //导入依赖的package包/类
@Test
public void testConnectionRetries() throws Exception {
HttpHosts mockHttpHosts = mock(HttpHosts.class);
ClientBase clientBase = new ClientBase("name",
mockClient, mockHttpHosts, new RawEndpoint("/endpoint", HttpConstants.HTTP_GET), mockAuth,
mockProcessor, mockReconnectionManager, mockRateTracker
);
BasicClient client = new BasicClient(clientBase, executorService);
final CountDownLatch latch = new CountDownLatch(1);
when(mockHttpHosts.nextHost())
.thenReturn("http://somehost.com");
when(mockClient.execute(any(HttpUriRequest.class)))
.thenReturn(mockResponse)
.thenReturn(mockResponse)
.thenThrow(new IOException())
.thenReturn(mockResponse);
when(mockStatusLine.getStatusCode())
.thenReturn(HttpConstants.Codes.UNAUTHORIZED)
.thenReturn(HttpConstants.Codes.SERVICE_UNAVAILABLE)
.thenReturn(HttpConstants.Codes.SUCCESS);
// turn off the client when we start processing
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
latch.countDown();
return null;
}
}).when(mockProcessor).process();
// for 401 Unauthorized
when(mockReconnectionManager.shouldReconnectOn400s()).thenReturn(true);
/** for shutdown **/
when(mockClient.getConnectionManager())
.thenReturn(mockConnectionManager);
assertFalse(clientBase.isDone());
client.connect();
latch.await();
client.stop();
assertTrue(client.isDone());
// exponential backoff twice: once for 401 once for 503
verify(mockReconnectionManager, times(2)).handleExponentialBackoff();
// for thrown IOException
verify(mockReconnectionManager).handleLinearBackoff();
// for successful connection
verify(mockReconnectionManager).resetCounts();
// finally start setting up processor/processing for the last attempt that goes through
verify(mockProcessor, atLeastOnce()).setup(any(InputStream.class));
verify(mockProcessor, atLeastOnce()).process();
assertEquals(EventType.STOPPED_BY_USER, clientBase.getExitEvent().getEventType());
verify(mockConnectionManager, atLeastOnce()).shutdown();
}