简体   繁体   中英

Google App Engine HTTP ERROR 503 error

Ï am Taking data From server written in "C" using Sockets . My java class name is ReceivingData, and here's the code for receiving the data and storing it in ArrayList and passing the ArrayList to other Class's Constructor.

package pack.exp;

import java.io.InputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class ReceivingData implements Runnable
{
    public static void main(String[] args) 
    {

    Thread t = new Thread(new ReceivingData());
         t.start();
    }

    public List<String> obj1;

    @Override
    public void run() 
    {
        Socket s;
        InputStream stream;
        try 
        {
            s = new Socket("10.9.211.22", 6870);
            stream = s.getInputStream();

            byte[] data = new byte[13];
            int read;
            String can_Id= null;

            while((read = stream.read(data)) != -1)
            {

                String can_Data= 
                                String.format("%02X:%02X:%02X:%02X, 
                                data[0], data[1], data[2], data[3]);


                List<String> obj1= new ArrayList<String>();
                obj1.add(can_Data.substring(0, 2));
                obj1.add(can_Data.substring(3, 5));
                obj1.add(can_Data.substring(6, 8));
                obj1.add(can_Data.substring(9, 11));


                Receiving_At_Regular_IntervalServlet rari= new 
                                Receiving_At_Regular_IntervalServlet(obj1);
                Thread.sleep(3000);
            }
        }

        catch (Exception e) 
        {

            e.printStackTrace();
        }
    }
 }

This is the Servlet which is receiving the data from ArrayList passed by the above File. and storing this data from the arraylist in to the Entity for datastore and deploys it on the Google App engine.

package pack.exp;


@SuppressWarnings("serial")
public class Receiving_At_Regular_IntervalServlet extends HttpServlet 
{
    List<String> obj2= new ArrayList<String>();

    public Receiving_At_Regular_IntervalServlet(List<String> obj2) throws 
        IOException 
    {
        this.obj2= obj2;
        System.out.println("Receiving in Web Project" + obj2);
        System.out.println("");
        System.out.println("");
    }

    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws 
        IOException 
    {
        Key k1 = KeyFactory.createKey("C","0D F0 0800 1"); 

       String parameter1 = obj2.get(0);

           Entity can1 = new Entity(k1);



         can1.setProperty("First Parameter", parameter1); 


         DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
         datastore.put(can1);


           Entity can11 = null;


        try 
        {
            can11= datastore.get(k1);

        }

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


        String first_P= (String) can11.getProperty("First Parameter");

        resp.setContentType("text/plain");
            resp.getWriter().println("Parameter---  " + first_P);  

   }
}

The ReceivingData code evidently runs a thread and reads data from 10.9.211.22 port 6870 using Socket from a local computer. That's fine. It converts four bytes to a List and passes that to Receiving_At_Regular_IntervalServlet. Fine but not what you need.

This part might work on a development computer but won't work if deployed to the cloud. AppEngine servers does not permit developers to define main(), use Socket or communicate with private IP subnet 10. Forget about deploying that code to AppEngine.

Receiving_At_Regular_IntervalServlet has a custom constructor. AppEngine does not call your constructor because its servlet code expects only the default constructor. That is probably when your 503 error occurs.

With servlets the data is not supposed to come in via a constructor. Data must come in via members of the request parameter of the doGet method (though to be RESTful you should rather use doPut in this example). You insert the data into the request parameter but sending a correctly constructed http request to the server. Your code lacks that web application design.

Build your main program and your AppEngine code in separate projects and make main talk to servlet using http.

HTTP ERROR 503 error

You can't help anything when a server throws this error. It is only thrown when a service from the server is unavailable.

You need explicit handling on such error codes, other than 200 OK , in the client app and appropriate message has to be shown or as the alternate requirement suggestion.

Refer to :

  1. Status Code definitions
  2. Java - 503 - SC_SERVICE_UNAVAILABLE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM