twitter4j.examples.async.AsyncUpdate Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of twitter4j-examples Show documentation
Show all versions of twitter4j-examples Show documentation
A Java library for the Twitter API
/*
* Copyright 2007 Yusuke Yamamoto
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package twitter4j.examples.async;
import twitter4j.AsyncTwitter;
import twitter4j.AsyncTwitterFactory;
import twitter4j.Status;
import twitter4j.TwitterAdapter;
import twitter4j.TwitterException;
import twitter4j.TwitterMethod;
/**
* This is a code example of Twitter4J async API.
* Usage: java twitter4j.examples.AsyncUpdate TwitterID TwitterPassword text
*
*
* @author Yusuke Yamamoto - yusuke at mac.com
*/
public final class AsyncUpdate {
/**
* Main entry for this application.
*
* @param args String[] TwitterID TwitterPassword StatusString
* @throws InterruptedException
*/
static final Object LOCK = new Object();
public static void main(String[] args) throws InterruptedException {
if (args.length < 1) {
System.out.println("Usage: java twitter4j.examples.AsyncUpdate text");
System.exit(-1);
}
AsyncTwitterFactory factory = new AsyncTwitterFactory();
AsyncTwitter twitter = factory.getInstance();
twitter.addListener(new TwitterAdapter() {
@Override
public void updatedStatus(Status status) {
System.out.println("Successfully updated the status to [" +
status.getText() + "].");
synchronized (LOCK) {
LOCK.notify();
}
}
@Override
public void onException(TwitterException e, TwitterMethod method) {
if (method == UPDATE_STATUS) {
e.printStackTrace();
synchronized (LOCK) {
LOCK.notify();
}
} else {
synchronized (LOCK) {
LOCK.notify();
}
throw new AssertionError("Should not happen");
}
}
});
twitter.updateStatus(args[0]);
synchronized (LOCK) {
LOCK.wait();
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy