Ocean West Posted May 1, 2012 Posted May 1, 2012 i am using this PostXMLData but is there any way to set a time out? sometimes some queries are just so long that there is no way to gracefully stop the process w/o force quitting. RegisterGroovy( "PostXMLData( url ; xml ; soapaction )" ; "// Send data¶ URL url = new URL(url);¶ HttpURLConnection conn = url.openConnection();¶ if( soapaction != null ) conn.setRequestProperty( \"SOAPAction\", soapaction );¶ conn.setRequestProperty( \"content-type\", \"text/xml\" );¶ conn.setDoOutput(true);¶ OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());¶ wr.write(xml);¶ wr.close();¶ ¶ // Get the response¶ String response;¶ InputStream responseStream;¶ try {¶ responseStream = conn.getInputStream();¶ success = 1;¶ } catch( IOException e ) {¶ success = 0;¶ if( conn.getResponseCode() == 500 ) {¶ responseStream = conn.getErrorStream();¶ } else throw e;¶ }¶ response = responseStream.getText(\"utf-8\");¶ responseStream.close();¶ return response;"; "isGui=false" )
FileMaker Magazine Posted May 16, 2012 Posted May 16, 2012 Anyone? Since I'm here reporting my issues with ScriptMaster. You can search google for this. "httpurlconnection setConnectTimeout" You'll come up with stuff like this http://stackoverflow.com/questions/6829801/httpurlconnection-setconnecttimeout-has-no-effect Essentially, right after conn.setRequestProperty( "content-type", "text/xml" );¶ You'll add this conn.setConnectTimeout(3000); Where the value is milliseconds per this http://docs.oracle.com/javase/1.5.0/docs/api/java/net/URLConnection.html#setConnectTimeout(int)
Cabinetman Posted October 10, 2012 Posted October 10, 2012 So I'm gonna bring this up again as I'm having an issue and wondering basically about the use of a ConnectTimeout ... So ....... Previously I used SM and another plugin (which I purchased from someone) together with FMP to connect to Amazons AWS PA API to retrieve XML data (product info, Amazon listing offers, etc.) with no problems. I ran the script hourly for testing, importing the offers part as records, with no issues I could tell of. Now I'm using SM to connect to Amazon's MWS API's using my own written modules (converted to a plugin using SM with a section of it posted at the very bottom of this post) without any overall problems. Well ..... with the exception of 1 that is. After an 'Install On Timer' script runs several times it eventually freezes, FM gives me the whitish screen and the spinning wheel thing. My only option is to forcibly close FM .... Questions : 1. Since I'm repeatedly connecting to Amazon and there exists the possibility that either my own internet might be down or they don't respond .... Should I somehow be testing the connection over and over? Should I have a ConnectTimeout within the SM Module? 2. I'm also wondering if it might be memory related. Am I not following a standard principle that I should be within my loop ? I might mention that SCRIPT 1 is set to 'Install On Timer' upon file opening. Basic example: <SCRIPT 1> set field .......... a few more set field set variable .......... a few more set variable preform script ["SCRIPT2"] <SCRIPT 2> set variable ......... a bunch more variables inbetween set variable go to layout delete records set variable [$whateverlist ; value:"list 1"] preform script ["SCRIPT 3"] set variable [$whateverlist ; value:"list 2"] preform script ["SCRIPT 3"] set variable [$whateverlist ; value:"list 3"] preform script ["SCRIPT 3"] <SCRIPT 3> Loop exit loop if [isempty ($whatever)] /* Note: I've found a Custom Function that I'm gonna use in the first 3 set variables called POP */ set variable [$whateverlist ; value:"some list"] set variable [$whateverlist2 ; value:"portion of my list"] set variable [$whateverlist ; value:"list less $whatever2"] /* end of note */ set variable [$url ; Value:MyFunctionFromMySMcreatedPlugin] /* Note here ... since FM wont connect to https I use the GetURLasText Module */ set field [table::myfield; GetURLasText ( $url ) ] export field contents import records End Loop </END SCRIPT 3> </END SCRIPT 2> preform script ["something"] .... do some things </END SCRIPT 1> Section of my module : POSTED WRONG MODULE ....
john renfrew Posted October 11, 2012 Posted October 11, 2012 Are you running this on a server or client?? This would presumably work better as a scheduled Server script than OnTimer?? My recent experience supporting another developer moving to MWS is that the Amazon infrastructure is extremely unstable and they do not even follow their own documentation (and I discovered several places inside their own functions with Java errors, there are 5 different copies of the PDF documentation on their website with differing information in each) - so we wrote our own functions from scratch. I would be testing everything you get returned from Amazon.
Cabinetman Posted October 11, 2012 Posted October 11, 2012 Are you running this on a server or client?? This would presumably work better as a scheduled Server script than OnTimer?? Client ... at least for now. If I decide to deploy this I really want the capability for each end user to run their own from their PC. My recent experience supporting another developer moving to MWS is that the Amazon infrastructure is extremely unstable and they do not even follow their own documentation (and I discovered several places inside their own functions with Java errors, there are 5 different copies of the PDF documentation on their website with differing information in each) - so we wrote our own functions from scratch. Oh you don't have to tell me! I've been working with it for about 1 1/2 yrs now. I have a very good overall understanding of how each API works and the best way to get what I need. The older MWS API's seem fine to me but the new Product API is far from it. Even if you can forget about the lack of data returned and their 'offer listing groups' you still find that the overall structure and stability compared to the AWS PA API is dismal. AND finally ... I posted the wrong Java module. I've dropped the connection style and gone to using java to create a signed url as the script shows. Sorry about that !! Ssssooooo ..... I guess since I'm using a SM module 'GetURLasText(url)' I need to look closer at that. Would this be about right ?? URL url = new URL(url); URLConnection conn = url.openConnection(); conn.setConnectTimeout(3000); InputStream input = conn.getInputStream(); //Determine character encoding. Default to utf-8 if it cannot be determined. String encoding = "utf-8"; String type = conn.getContentType(); if( type != null ) { int n = type.toLowerCase().indexOf("charset="); if( n != -1 ) { n += "charset=".length(); encoding = type.substring( n ).replace(""", ""); } } try { StringBuffer sb = new StringBuffer( input.available() ); Reader r = new InputStreamReader( input, encoding ); char[] buff = new char[2048]; int charsRead; while( (charsRead=r.read( buff )) != -1 ) { sb.append( buff, 0, charsRead ); } return sb.toString(); } finally { input.close(); }
john renfrew Posted October 13, 2012 Posted October 13, 2012 encoding = type.substring( n ).replace('"', '') and also conn.setReadTimeout(3000) ??
Cabinetman Posted October 13, 2012 Posted October 13, 2012 encoding = type.substring( n ).replace('"', '') and also conn.setReadTimeout(3000) ?? Sorry for my lack of understanding but I don't quite follow ... or want to clarify. The code comes from SM as far as : Also I don't have a .setReadTimeout ... it's a .setConnectTimeout. So again are you suggesting to add it or replace? encoding = type.substring( n ).replace('"', '') So are you saying add in or replace with? encoding = type.substring( n ).replace(""", "");
john renfrew Posted October 13, 2012 Posted October 13, 2012 The set connect timeout does NOT account for the fact that the server may be slow in responding back to you after you have connected, so the addition of a setReadTimeout will help in the case of the Amazon infrastructure, which is flaky at best. I am suggesting replacing the .replace code with the one in the post. It makes better use of groovy conventions and makes it a WHOLE lots easier to troubleshoot as ' remains the same while FM escapes any " into " There is NO change to performance, but just like not using ; at the end of each line or adding java classes to the start of each line when it is unambiguous (Groovy does a brilliant job of the coercion) it makes the code cleaner and simpler to read by removing fluff which is functionally unnecessary. You can use pure Java code grabbed from these functions or the inter-web, but IMHO code written as well formed Groovy is just more lovely. e.g. for (int i; i == test; i++) or for ( i in 0..test ) Easier to read, less typing, and no extra (required) punctuation to miss out! And really easy to test iterate just once (or twice) - for (i in 0..0) rather than change the test..
Recommended Posts
This topic is 4491 days old. Please don't post here. Open a new topic instead.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now