我正在尝试将来自 Sample Stream 的推文存储到数据库中并同时存储原始 json。我正在使用hbc Github 存储库中Twitter4jStatusClient
的示例。由于我只是将信息的子集实时存储到数据库中,因此我希望也存储推文的原始 json,以便在需要时检索其他信息。但是 usingTwitter4jStatusClient
意味着侦听器在不同的线程上执行,并且在这里,它表示为了获取 json 对象,它必须从检索 json 对象的同一线程中执行。使用时有没有办法保存 json 字符串Twitter4JStatusClient
?我选择不使用这个例子因为我只想执行某些操作并保存 json 字符串(如果它是一个状态)。谢谢!
// 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);