I am trying to store the tweets from Sample Stream into a database and store the raw json at the same time. I am using Twitter4jStatusClient
following the example in hbc Github repository. Since I am only storing a subset of information into the database at real time, I am hoping to store the raw json of the tweet as well so that I may retrieve additional information when I need it. However using Twitter4jStatusClient
means that the listener is executed on a different thread, and in here, it says that in order to get the json object, it must be executed from the same thread that retrieved the json object. Is there a way of saving the json string when using Twitter4JStatusClient
? I chose not to use this example because i only wanted to perform certain actions and save the json string if it is a status. Thanks!
// Create an appropriately sized blocking queue
BlockingQueue<String> queue = new LinkedBlockingQueue<String>(10000);
// Define our endpoint: By default, delimited=length is set (we need this for our processor)
// and stall warnings are on.
StatusesSampleEndpoint endpoint = new StatusesSampleEndpoint();
// Specify the language filter for the endpoint
endpoint.addQueryParameter(Constants.LANGUAGE_PARAM, Joiner.on(',').join(Lists.newArrayList("en")));
endpoint.stallWarnings(false);
Authentication auth = new OAuth1(consumerKey, consumerSecret, token, secret);
// Create a new BasicClient. By default gzip is enabled.
BasicClient client = new ClientBuilder()
.name("sampleStreamClient")
.hosts(Constants.STREAM_HOST)
.endpoint(endpoint)
.authentication(auth)
.processor(new StringDelimitedProcessor(queue))
.build();
// Create an executor service which will spawn threads to do the actual work of parsing the incoming messages and
// calling the listeners on each message
int numProcessingThreads = 4;
ExecutorService service = Executors.newFixedThreadPool(numProcessingThreads);
StatusListener listener = new SampleStreamStatusListener(jsonInserter);
// Wrap our BasicClient with the twitter4j client
t4jClient = new Twitter4jStatusClient(
client, queue, Lists.newArrayList(listener), service);