Async URL Task

Discussion in 'Plugin Development' started by Hydrosis, Jan 14, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Hydrosis

    Hello, I'm wondering if it's possible to run a code that will return a string value asynchronously from a certain URL. The server hangs noticeably when I try to get a CSV from it, so I don't think running it in the main thread is very efficient. Here is what I have now:

    Code:java
    1. public String getData(String symbol) throws MalformedURLException, IOException
    2. {
    3. String bid = null;
    4. InputStream input = null;
    5. Reader reader = null;
    6. input = new URL("blah blah url"+symbol).openStream();
    7. reader = new InputStreamReader(input, "UTF-8");
    8. CSVReader info = new CSVReader(reader);
    9. bid = info.readNext()[0];
    10. info.close();
    11. return bid==null ? "Error" : bid;
    12.  
    13. }


    Any suggestions on how to make this more efficient? :)
     
  2. Offline

    xTrollxDudex

    Hydrosis
    Start a new Thread for it.
    PHP:
    private class Task extends Thread{
        public 
    void run() {
            
    // Do your code here
        
    }
    }

    new 
    Task().start();
     
  3. Offline

    AmShaegar

    xTrollxDudex is right, you should use a new thread for this. Although I'd recommend to use Bukkit API for this. The problem is that your code example cannot be easily transformed into a new thread because a thread will not return String. You need to begin earlier.
    Code:java
    1. Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() {
    2.  
    3. @Override
    4. public void run() {
    5. String data = getData(symbol);
    6. // do something with data here
    7. }
    8. };

    In an async task you are not allowed to use Bukkit API. That means you should not change blocks, teleport players etc. This can crash your server. If you want to access Bukkit API again you need to run a sync task again.
    Code:java
    1. Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() {
    2.  
    3. @Override
    4. public void run() {
    5. final String data = getData(symbol);
    6. Bukkit.getScheduler().runTask(plugin, new Runnable() {
    7.  
    8. @Override
    9. public void run() {
    10. // you are allowed to access API here
    11. }
    12. });
    13. }
    14. };

    data has to be final then to be accessible inside the new task. So has every other outer variable to be if you need to access them from inside a task.
     
    NathanWolf likes this.
  4. Offline

    Hydrosis

    xTrollxDudex
    How could I use that to return a value (such as a string)? Could you type a quick example of that? :)

    AmShaegar
    Thanks, that works for me!
     
  5. Offline

    xTrollxDudex

    Hydrosis
    You could do what AmShaeger did or you can use a reference String each time a new Task is created, if you do use this method, make sure to lock down the field before setting it.
     
Thread Status:
Not open for further replies.

Share This Page