+ Start a Discussion
thangasan@yahoothangasan@yahoo 

Problem With Bulk API Enterprise API in SandBox Environment

Hai

 

  I am trying Bulk API Enterprise WSD in SandBox the below error is coming

 

 Exception in thread "main" [AsyncApiException  exceptionCode='ClientInputError'
 exceptionMessage='Failed to create job '
]

 at com.sforce.async.RestConnection.createOrUpdateJob(RestConnection.java:102)
 at com.sforce.async.RestConnection.createJob(RestConnection.java:76)
 at client.BulkClient.createJob(BulkClient.java:162)
 at client.BulkClient.runSample(BulkClient.java:56)
 at client.BulkClient.main(BulkClient.java:45)
Caused by: java.net.NoRouteToHostException: No route to host: connect
 at java.net.PlainSocketImpl.socketConnect(Native Method)
 at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
 at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
 at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
 at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
 at java.net.Socket.connect(Socket.java:519)
 at com.sun.net.ssl.internal.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:550)
 at com.sun.net.ssl.internal.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:141)
 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.protocol.https.HttpsClient.<init>(HttpsClient.java:271)
 at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:328)
 at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:172)
 at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:816)
 at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:158)
 at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:881)
 at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:230)
 at com.sforce.ws.transport.JdkHttpTransport.connectLocal(JdkHttpTransport.java:132)
 at com.sforce.ws.transport.JdkHttpTransport.connect(JdkHttpTransport.java:63)
 at com.sforce.async.RestConnection.createOrUpdateJob(RestConnection.java:82)
 ... 4 more

 

Source

 

package client;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.xml.rpc.ServiceException;

import com.sforce.async.AsyncApiException;
import com.sforce.async.BatchInfo;
import com.sforce.async.BatchStateEnum;
import com.sforce.async.CSVReader;
import com.sforce.async.ContentType;
import com.sforce.async.JobInfo;
import com.sforce.async.JobStateEnum;
import com.sforce.async.OperationEnum;
import com.sforce.async.RestConnection;
import com.sforce.soap.enterprise.LoginResult;
import com.sforce.soap.enterprise.SessionHeader;
import com.sforce.soap.enterprise.SforceServiceLocator;
import com.sforce.soap.enterprise.SoapBindingStub;
import com.sforce.soap.enterprise.fault.ExceptionCode;
import com.sforce.soap.enterprise.fault.LoginFault;
import com.sforce.ws.ConnectionException;
import com.sforce.ws.ConnectorConfig;

public class BulkClient {
 private LoginResult loginResult;

 /**
  * @param args
  */
 public static void main(String[] args) throws ServiceException,AsyncApiException,
   ConnectionException, IOException {
  BulkClient bc = new BulkClient();
  bc.runSample("Account", "userID", "password", "Sample.csv");
 }

 /**
  * Creates a Bulk API. job and uploads batches for a CSV file.
  */
 public void runSample(String sobjectType, String userName, String password,
   String sampleFileName) throws ServiceException,AsyncApiException,
   ConnectionException, IOException {
  login(userName, password);
  RestConnection connection = getRestConnection();
  JobInfo job = createJob(sobjectType, connection);
  List<BatchInfo> batchInfoList = createBatchesFromCSVFile(connection,
    job, sampleFileName);
  closeJob(connection, job.getId());
  awaitCompletion(connection, job, batchInfoList);
  checkResults(connection, job, batchInfoList);
 }

 private boolean login(String userName, String password) throws ServiceException {
  SoapBindingStub binding = (SoapBindingStub) new SforceServiceLocator().getSoap();

  // Time out after a minute
  binding.setTimeout(60000);
  System.getProperties().put("proxySet", "true");
  System.getProperties().put("http.proxyHost", "proxy");
  System.getProperties().put("http.proxyPort", "8080");

  try {
   System.out.println("LOGGING IN NOW....");
   loginResult = binding.login(userName, password);
  } catch (LoginFault ex) {
   // The LoginFault derives from AxisFault
   ExceptionCode exCode = ex.getExceptionCode();
   if (exCode == ExceptionCode.FUNCTIONALITY_NOT_ENABLED
     || exCode == ExceptionCode.INVALID_CLIENT
     || exCode == ExceptionCode.INVALID_LOGIN
     || exCode == ExceptionCode.LOGIN_DURING_RESTRICTED_DOMAIN
     || exCode == ExceptionCode.LOGIN_DURING_RESTRICTED_TIME
     || exCode == ExceptionCode.ORG_LOCKED
     || exCode == ExceptionCode.PASSWORD_LOCKOUT
     || exCode == ExceptionCode.SERVER_UNAVAILABLE
     || exCode == ExceptionCode.TRIAL_EXPIRED
     || exCode == ExceptionCode.UNSUPPORTED_CLIENT) {
    System.out
      .println("Please be sure that you have a valid username "
        + "and password.");
   } else {
    // Write the fault code to the console
    System.out.println(ex.getExceptionCode());
    // Write the fault message to the console
    System.out.println("An unexpected error has occurred."
      + ex.getMessage());
   }
   return false;
  } catch (Exception ex) {
   System.out.println("An unexpected error has occurred: "
     + ex.getMessage());
   ex.printStackTrace();
   return false;
  }
  // Check if the password has expired
  if (loginResult.isPasswordExpired()) {
   System.out
     .println("An error has occurred. Your password has expired.");
   return false;
  }
  binding._setProperty(SoapBindingStub.ENDPOINT_ADDRESS_PROPERTY,
    loginResult.getServerUrl());
  System.out.println(loginResult.getServerUrl());
  SessionHeader sh = new SessionHeader();
  sh.setSessionId(loginResult.getSessionId());
  binding.setHeader(new SforceServiceLocator().getServiceName()
    .getNamespaceURI(), "SessionHeader", sh);
  return true;
 }

 /**
  * Create the RestConnection used to call Bulk API operations.
  */
 private RestConnection getRestConnection()
   throws ConnectionException, AsyncApiException {

  ConnectorConfig config = new ConnectorConfig();
  config.setSessionId(loginResult.getSessionId());
  String soapEndpoint = loginResult.getServerUrl();
  String apiVersion = "17.0";
  String restEndpoint = soapEndpoint.substring(0, soapEndpoint
    .indexOf("Soap/"))
    + "async/" + apiVersion;
  System.out.println(restEndpoint);
  config.setRestEndpoint(restEndpoint);
  config.setCompression(true);
  config.setTraceMessage(false);
  RestConnection connection = new RestConnection(config);
  return connection;
 }

 /**
  * Create a new job using the Bulk API.
  **
  * @param sobjectType
  *            The object type being loaded, such as "Account"
  * @param connection
  *            RestConnection used to create the new job.
  * @return The JobInfo for the new job.
  * @throws AsyncApiException
  */
 private JobInfo createJob(String sobjectType, RestConnection connection)
   throws AsyncApiException {
  JobInfo job = new JobInfo();
  job.setObject(sobjectType);
  job.setOperation(OperationEnum.insert);
  job.setContentType(ContentType.CSV);
//  System.getProperties().put("proxySet", "true");
//  System.getProperties().put("http.proxyHost", "proxy");
//  System.getProperties().put("http.proxyPort", "8080");
  job = connection.createJob(job);
  System.out.println(job);
  return job;
 }

 private List<BatchInfo> createBatchesFromCSVFile(RestConnection connection,
   JobInfo jobInfo, String csvFileName) throws IOException,
   AsyncApiException {
  List<BatchInfo> batchInfos = new ArrayList<BatchInfo>();
  BufferedReader rdr = new BufferedReader(new InputStreamReader(
    new FileInputStream(csvFileName)));
  // read the CSV header row
  byte[] headerBytes = (rdr.readLine() + "\n").getBytes("UTF-8");
  int headerBytesLength = headerBytes.length;
  File tmpFile = File.createTempFile("bulkAPIInsert", ".csv");
  // Split the CSV file into multiple batches
  try {
   FileOutputStream tmpOut = new FileOutputStream(tmpFile);
   int maxBytesPerBatch = 10000000; // 10 million bytes per batch
   int maxRowsPerBatch = 10000; // 10 thousand rows per batch
   int currentBytes = 0;
   int currentLines = 0;
   String nextLine;
   while ((nextLine = rdr.readLine()) != null) {
    byte[] bytes = (nextLine + "\n").getBytes("UTF-8");
    // Create a new batch when our batch size limit is reached
    if (currentBytes + bytes.length > maxBytesPerBatch
      || currentLines > maxRowsPerBatch) {
     createBatch(tmpOut, tmpFile, batchInfos, connection,
       jobInfo);
     currentBytes = 0;
     currentLines = 0;
    }
    if (currentBytes == 0) {
     tmpOut = new FileOutputStream(tmpFile);
     tmpOut.write(headerBytes);
     currentBytes = headerBytesLength;
     currentLines = 1;
    }
    tmpOut.write(bytes);
    currentBytes += bytes.length;
    currentLines++;
   }
   // Finished processing all rows
   // Create a final batch for any remaining data
   if (currentLines > 1) {
    createBatch(tmpOut, tmpFile, batchInfos, connection, jobInfo);
   }
  } finally {
   tmpFile.delete();
  }
  return batchInfos;
 }

 private void createBatch(FileOutputStream tmpOut, File tmpFile,
   List<BatchInfo> batchInfos, RestConnection connection,
   JobInfo jobInfo) throws IOException, AsyncApiException {
  tmpOut.flush();
  tmpOut.close();
  FileInputStream tmpInputStream = new FileInputStream(tmpFile);
  try {
   BatchInfo batchInfo = connection.createBatchFromStream(jobInfo,
     tmpInputStream);
   System.out.println(batchInfo);
   batchInfos.add(batchInfo);
  } finally {
   tmpInputStream.close();
  }
 }

 private void closeJob(RestConnection connection, String jobId)
   throws AsyncApiException {
  JobInfo job = new JobInfo();
  job.setId(jobId);
  job.setState(JobStateEnum.Closed);
  connection.updateJob(job);
 }

 private void awaitCompletion(RestConnection connection, JobInfo job,
   List<BatchInfo> batchInfoList) throws AsyncApiException {
  long sleepTime = 0L;
  Set<String> incomplete = new HashSet<String>();
  for (BatchInfo bi : batchInfoList) {
   incomplete.add(bi.getId());
  }
  while (!incomplete.isEmpty()) {
   try {
    Thread.sleep(sleepTime);
   } catch (InterruptedException e) {
   }
   System.out.println("Awaiting results..." + incomplete.size());
   sleepTime = 10000L;
   BatchInfo[] statusList = connection.getBatchInfoList(job.getId())
     .getBatchInfo();
   for (BatchInfo b : statusList) {
    if (b.getState() == BatchStateEnum.Completed
      || b.getState() == BatchStateEnum.Failed) {
     if (incomplete.remove(b.getId())) {
      System.out.println("BATCH STATUS:\n" + b);
     }
    }
   }
  }
 }

 private void checkResults(RestConnection connection, JobInfo job,
   List<BatchInfo> batchInfoList) throws AsyncApiException,
   IOException {
  // batchInfoList was populated when batches were created and submitted
  for (BatchInfo b : batchInfoList) {
   CSVReader rdr = new CSVReader(connection.getBatchResultStream(job
     .getId(), b.getId()));
   List<String> resultHeader = rdr.nextRecord();
   int resultCols = resultHeader.size();
   List<String> row;
   while ((row = rdr.nextRecord()) != null) {
    Map<String, String> resultInfo = new HashMap<String, String>();
    for (int i = 0; i < resultCols; i++) {
     resultInfo.put(resultHeader.get(i), row.get(i));
    }
    boolean success = Boolean.valueOf(resultInfo.get("Success"));
    boolean created = Boolean.valueOf(resultInfo.get("Created"));
    String id = resultInfo.get("Id");
    String error = resultInfo.get("Error");
    if (success && created) {
     System.out.println("Created row with id " + id);
    } else if (!success) {
     System.out.println("Failed with error: " + error);
    }
   }
  }
 }

}

In the above Source code Login is OK, after that creating Job with Restful connection error occured

Bulk API need to set Proxy ?? ,Because i am running this application in Proxy network.

 

 

 

Please Help

 

Thanks and Regards

Thanga

Message Edited by thangasan@yahoo on 10-18-2009 11:24 PM
Best Answer chosen by Admin (Salesforce Developers) 
thangasan@yahoothangasan@yahoo

Solved after set the Proxy in

config.setProxy("Proxy", 8080);

 

Thanga

All Answers

thangasan@yahoothangasan@yahoo

Solved after set the Proxy in

config.setProxy("Proxy", 8080);

 

Thanga

This was selected as the best answer
Salesforce_Developer.ax1278Salesforce_Developer.ax1278

 

    I am getting the same error and i am not using any proxy server. What could be the problem ?

 

Does ant-virus firewall causing a problem or any other security certificate issue ?