Skip to content

Commit f7f5c2d

Browse files
committed
adding logging instead of system.out or .err
1 parent 39e3261 commit f7f5c2d

File tree

6 files changed

+55
-39
lines changed

6 files changed

+55
-39
lines changed

examples/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,10 @@
1919
<version>1.2.4</version>
2020
<scope>compile</scope>
2121
</dependency>
22+
<dependency>
23+
<groupId>org.slf4j</groupId>
24+
<artifactId>slf4j-simple</artifactId>
25+
<version>1.7.5</version>
26+
</dependency>
2227
</dependencies>
2328
</project>

examples/src/main/java/com/twitter/clientlib/HelloWorldStreaming.java

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,7 @@
2525

2626
import com.twitter.clientlib.model.*;
2727
import com.twitter.clientlib.query.StreamQueryParameters;
28-
import com.twitter.clientlib.query.model.MediaField;
29-
import com.twitter.clientlib.query.model.PlaceField;
30-
import com.twitter.clientlib.query.model.PollField;
3128
import com.twitter.clientlib.query.model.TweetField;
32-
import com.twitter.clientlib.query.model.UserField;
3329
import com.twitter.clientlib.stream.TweetsStreamListener;
3430
import com.twitter.clientlib.stream.TwitterStream;
3531

@@ -48,17 +44,9 @@ public static void main(String[] args) {
4844
twitterStream.setTwitterCredentials(credentials);
4945
twitterStream.addListener(new Responder());
5046

51-
// twitterStream.sampleStream(new StreamQueryParameters.Builder()
52-
// .withTweetFields(TweetField.AUTHOR_ID, TweetField.ID, TweetField.CREATED_AT)
53-
// .build());
54-
5547
twitterStream.sampleStream(new StreamQueryParameters.Builder()
56-
.withTweetFields(TweetField.all())
57-
.withMediaFields(MediaField.all())
58-
.withUserFields(UserField.all())
59-
.withPollFields(PollField.all())
60-
.withPlaceFields(PlaceField.all())
61-
.build());
48+
.withTweetFields(TweetField.AUTHOR_ID, TweetField.ID, TweetField.CREATED_AT)
49+
.build());
6250

6351
}
6452
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.twitter.clientlib.exceptions;
2+
3+
public class StreamException extends RuntimeException{
4+
5+
public StreamException(String message) {
6+
super(message);
7+
}
8+
9+
public StreamException(String message, Throwable cause) {
10+
super(message, cause);
11+
}
12+
}

src/main/java/com/twitter/clientlib/stream/TweetsStreamExecutor.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,18 @@
3434

3535
import com.fasterxml.jackson.core.JsonProcessingException;
3636
import com.fasterxml.jackson.databind.DeserializationFeature;
37-
import com.fasterxml.jackson.databind.JsonMappingException;
3837
import com.fasterxml.jackson.databind.ObjectMapper;
3938

4039
import com.twitter.clientlib.exceptions.EmptyStreamTimeoutException;
4140
import com.twitter.clientlib.model.StreamingTweet;
4241
import okio.BufferedSource;
42+
import org.slf4j.Logger;
43+
import org.slf4j.LoggerFactory;
4344

4445
public class TweetsStreamExecutor {
4546

47+
private static final Logger logger = LoggerFactory.getLogger(TweetsStreamExecutor.class);
48+
4649
private static final long EMPTY_STREAM_TIMEOUT = 20000;
4750
private static final int POLL_WAIT = 5;
4851
private volatile BlockingQueue<String> rawTweets;
@@ -75,7 +78,7 @@ public void removeListener(TweetsStreamListener toRemove) {
7578

7679
public void start() {
7780
if (stream == null) {
78-
System.out.println("Error: stream is null.");
81+
logger.error("Stream is null. Exiting...");
7982
return;
8083
}
8184
startTime = System.currentTimeMillis();
@@ -91,6 +94,7 @@ public void start() {
9194
}
9295

9396
public synchronized void shutdown() {
97+
logger.info("TweetsStreamListenersExecutor is shutting down.");
9498
isRunning = false;
9599
shutDownServices();
96100
try {
@@ -102,7 +106,6 @@ public synchronized void shutdown() {
102106
} catch (IOException e) {
103107

104108
}
105-
System.out.println("TweetsStreamListenersExecutor is shutting down.");
106109
}
107110

108111
private void shutDownServices() {
@@ -120,12 +123,14 @@ private void terminateService(ExecutorService executorService) throws Interrupte
120123
if (!executorService.awaitTermination(1500, TimeUnit.MILLISECONDS)) {
121124
executorService.shutdownNow();
122125
if (!executorService.awaitTermination(1500, TimeUnit.MILLISECONDS))
123-
System.err.println("Pool did not terminate");
126+
logger.error("Thread pool did not terminate");
124127
}
125128
}
126129

127130
private class RawTweetsQueuer implements Runnable {
128131

132+
private final Logger logger = LoggerFactory.getLogger(RawTweetsQueuer.class);
133+
129134
@Override
130135
public void run() {
131136
queueTweets();
@@ -159,13 +164,15 @@ public void queueTweets() {
159164
}
160165
}
161166
} catch (Exception e) {
162-
System.out.println("Something went wrong. Closing stream... " + e.getMessage());
167+
logger.error("Something went wrong. Closing stream... {}", e.getMessage());
163168
shutdown();
164169
}
165170
}
166171
}
167172

168173
private class DeserializeTweetsTask implements Runnable {
174+
175+
private final Logger logger = LoggerFactory.getLogger(DeserializeTweetsTask.class);
169176
private final ObjectMapper objectMapper;
170177

171178
private DeserializeTweetsTask() {
@@ -181,16 +188,18 @@ public void run() {
181188
if (rawTweet == null) continue;
182189
StreamingTweet tweet = objectMapper.readValue(rawTweet, StreamingTweet.class);
183190
tweets.put(tweet);
184-
} catch (InterruptedException e) {
191+
} catch (InterruptedException ignore) {
185192

186193
} catch (JsonProcessingException e) {
187-
System.out.println("debug log here");
194+
logger.debug("Json could not be parsed");
188195
}
189196
}
190197
}
191198
}
192199

193200
private class TweetsListenersTask implements Runnable {
201+
202+
private final Logger logger = LoggerFactory.getLogger(TweetsListenersTask.class);
194203
@Override
195204
public void run() {
196205
processTweets();
@@ -211,7 +220,7 @@ private void processTweets() {
211220
long stopTime = System.currentTimeMillis();
212221
long durationInMillis = stopTime - startTime;
213222
double seconds = durationInMillis / 1000.0;
214-
System.out.println("Total duration in seconds: " + seconds);
223+
logger.info("Total duration in seconds: {}", seconds);
215224
shutdown();
216225
}
217226
} catch (InterruptedException e) {

src/main/java/com/twitter/clientlib/stream/TwitterStream.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,27 @@
55
import com.twitter.clientlib.TwitterCredentialsBearer;
66
import com.twitter.clientlib.api.TweetsApi;
77
import com.twitter.clientlib.exceptions.AuthenticationException;
8+
import com.twitter.clientlib.exceptions.StreamException;
89
import com.twitter.clientlib.query.StreamQueryParameters;
10+
911
import okio.BufferedSource;
12+
import org.slf4j.Logger;
13+
import org.slf4j.LoggerFactory;
1014

1115
import java.util.LinkedList;
1216
import java.util.List;
1317

1418
public class TwitterStream {
19+
20+
private static final Logger logger = LoggerFactory.getLogger(TwitterStream.class);
1521
private final TweetsApi tweets = new TweetsApi();
1622

1723
private final ApiClient apiClient = new ApiClient();
1824

1925
private final List<TweetsStreamListener> listeners = new LinkedList<>();
2026

27+
private TweetsStreamExecutor executor;
28+
2129
public TwitterStream() {
2230
initBasePath();
2331
tweets.setClient(apiClient);
@@ -34,16 +42,22 @@ public void addListener(TweetsStreamListener listener) {
3442
public void sampleStream(StreamQueryParameters streamParameters) {
3543
try {
3644
BufferedSource streamResult = tweets.sampleStream(streamParameters);
37-
TweetsStreamExecutor executor = new TweetsStreamExecutor(streamResult);
45+
executor = new TweetsStreamExecutor(streamResult);
3846
listeners.forEach(executor::addListener);
3947
executor.start();
4048
} catch (ApiException e) {
4149
if(e.getCode() == 401) {
50+
logger.error("Authentication didn't work");
4251
throw new AuthenticationException("Not authenticated. Please check the credentials", e);
4352
}
53+
throw new StreamException("An exception occurred during stream execution", e);
4454
}
4555
}
4656

57+
public void shutdown() {
58+
executor.shutdown();
59+
}
60+
4761
private void initBasePath() {
4862
String basePath = System.getenv("TWITTER_API_BASE_PATH");
4963
apiClient.setBasePath(basePath != null ? basePath : "https://api.twitter.com");

src/test/java/com/twitter/clientlib/api/TweetsApiTest.java

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
5454
import com.twitter.clientlib.model.UsersRetweetsCreateRequest;
5555
import com.twitter.clientlib.model.UsersRetweetsCreateResponse;
5656
import com.twitter.clientlib.model.UsersRetweetsDeleteResponse;
57+
import com.twitter.clientlib.query.StreamQueryParameters;
58+
import okio.BufferedSource;
5759
import org.junit.jupiter.api.Disabled;
5860
import org.junit.jupiter.api.Test;
5961

@@ -248,14 +250,7 @@ public void listsIdTweetsTest() throws ApiException {
248250
*/
249251
@Test
250252
public void sampleStreamTest() throws ApiException {
251-
Set<String> expansions = null;
252-
Set<String> tweetFields = null;
253-
Set<String> userFields = null;
254-
Set<String> mediaFields = null;
255-
Set<String> placeFields = null;
256-
Set<String> pollFields = null;
257-
Integer backfillMinutes = null;
258-
InputStream response = apiInstance.tweets().sampleStream(expansions, tweetFields, userFields, mediaFields, placeFields, pollFields, backfillMinutes);
253+
BufferedSource response = apiInstance.tweets().sampleStream(new StreamQueryParameters.Builder().build());
259254
// TODO: test validations
260255
}
261256

@@ -269,14 +264,7 @@ public void sampleStreamTest() throws ApiException {
269264
*/
270265
@Test
271266
public void searchStreamTest() throws ApiException {
272-
Set<String> expansions = null;
273-
Set<String> tweetFields = null;
274-
Set<String> userFields = null;
275-
Set<String> mediaFields = null;
276-
Set<String> placeFields = null;
277-
Set<String> pollFields = null;
278-
Integer backfillMinutes = null;
279-
InputStream response = apiInstance.tweets().searchStream(expansions, tweetFields, userFields, mediaFields, placeFields, pollFields, backfillMinutes);
267+
BufferedSource response = apiInstance.tweets().searchStream(new StreamQueryParameters.Builder().build());
280268
// TODO: test validations
281269
}
282270

0 commit comments

Comments
 (0)