1

JSON データをサーバーに投稿しようとしていますが、次のエラーが発生します。

Thread 17 got Exception: java.net.SocketException: Invalid argument
java.net.SocketException: Invalid argument
    at java.net.PlainSocketImpl.socketSetOption(Native Method)
    at java.net.PlainSocketImpl.setOption(PlainSocketImpl.java:282)
    at java.net.Socket.setTcpNoDelay(Socket.java:864)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:402)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:529)
    at sun.net.www.http.HttpClient.<init>(HttpClient.java:233)
    at sun.net.www.http.HttpClient.New(HttpClient.java:306)
    at sun.net.www.http.HttpClient.New(HttpClient.java:323)
    at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:970)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:911)
    at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:836)
    at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1014)
    at Job.run(RestApiReader.java:74)
    at java.lang.Thread.run(Thread.java:680)
Thread 20 got Exception: java.net.SocketException: Connection reset by peer
java.net.SocketException: Connection reset by peer
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:432)
    at java.net.Socket.connect(Socket.java:529)
    at java.net.Socket.connect(Socket.java:478)
    at sun.net.NetworkClient.doConnect(NetworkClient.java:163)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:394)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:529)
    at sun.net.www.http.HttpClient.<init>(HttpClient.java:233)
    at sun.net.www.http.HttpClient.New(HttpClient.java:306)
    at sun.net.www.http.HttpClient.New(HttpClient.java:323)
    at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:970)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:911)
    at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:836)
    at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1014)
    at Job.run(RestApiReader.java:74)
    at java.lang.Thread.run(Thread.java:680)

関連するコードは次のとおりです。

        URL url = new URL(post_api_url);
        HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
        httpConnection.setDoOutput(true);
        httpConnection.setDoInput(true);

        httpConnection.setRequestMethod("POST");
        httpConnection.setRequestProperty("Accept","application/json");
        httpConnection.setRequestProperty("Content-type","application/json");

        DataOutputStream wr = new DataOutputStream(httpConnection.getOutputStream());
        wr.writeBytes(parameters);
        wr.flush();
        wr.close();

コード全体:

/* The following dependencies are required
Apache Common
JsonSimple 
MySQL Drivers
*/

import java.sql.*;
import java.io.IOException;
import java.io.DataOutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.json.simple.JSONObject;

class Job implements Runnable
{
    private  Connection connection = null;
    private  ResultSet  resultset  = null;
    private  Statement  statement  = null;
    private  int age_limit;
    private  String post_api_url;
    private  String query;

    public Job(int age_limit) throws SQLException
    {
        this.age_limit = age_limit;
        this.post_api_url = "http://localhost:3000/users";
        this.connection = DriverManager.getConnection("jdbc:mysql://localhost/rest_source?"
                    + "user=root&password=somethingsomething");
        this.query = "Select * from users where age >= "+(age_limit-10)+" AND age<= "+age_limit;
        this.statement = connection.createStatement();
    }

    public void run() 
    { 
        try
        {    

            resultset = statement.executeQuery(query);

            while (resultset.next())
            {

                String name  =  resultset.getString("name");
                String email =  resultset.getString("email");
                int age   =  resultset.getInt("age");
                String city  =  resultset.getString("city");
                long client_thread_id = Thread.currentThread().getId();

                //Make a JSon Object
                JSONObject top = new JSONObject();

                JSONObject jsonobject = new JSONObject();
                jsonobject.put("name",name);
                jsonobject.put("email",email);
                jsonobject.put("age",age);
                jsonobject.put("city",city);
                jsonobject.put("client_thread_id",client_thread_id);

                top.put("user",jsonobject);

                String parameters = top.toString();

                //Make the POST request 

                URL url = new URL(post_api_url);
                HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
                httpConnection.setDoOutput(true);
                httpConnection.setDoInput(true);

                httpConnection.setRequestMethod("POST");
                httpConnection.setRequestProperty("Accept","application/json");
                httpConnection.setRequestProperty("Content-type","application/json");

                DataOutputStream wr = new DataOutputStream(httpConnection.getOutputStream());
                wr.writeBytes(parameters);
                wr.flush();
                wr.close();

                Thread.yield(); //Play nice and give turns
            }

            resultset.close();
            resultset = null;

            statement.close();
            statement = null;

            if ( connection != null )
            {
               connection.close();
               connection = null;
            }

            System.out.println("Thread " + Thread.currentThread().getId() +  " is finished. ");
        }
        catch (Exception e)
        {
            System.out.println("Thread " + Thread.currentThread().getId() + " got Exception: " + e);
            e.printStackTrace();
            return;
        }
    }
}


public class RestApiReader
{
    private static int NUM_OF_THREADS = 10;

    public static void main (String args [])
    {
          try  
          {  
                Class.forName("com.mysql.jdbc.Driver");

                Thread threadList[] = new Thread[NUM_OF_THREADS];

                for (int i = 0; i < NUM_OF_THREADS; i++)
                {
                    threadList[i] = new Thread(new Job((i+1)*10));
                    threadList[i].start();
                }

                for (int i = 0; i < NUM_OF_THREADS; i++)
                {
                    threadList[i].join();
                }

                System.out.println("All Jobs done...");

          }
          catch (Exception e)
          {
                e.printStackTrace();
          }

    }  

}
4

0 に答える 0