Salesforce wsc hacking: adding RequestTimedOutException

I have a number of situations where a RequestTimedOutException would be appropriate instead of just a ConnectionException when SoapConnection.send() encounters a SocketTimeoutException.

This would ease retry implementations (related to timeouts) in that they would only have to consider a special type of exception and does not have to revert to parsing exception messages.

If a RequestTimedOutException extends ConnectionException the handling of SocketTimeoutException in the SoapConnection class can be done without changing the api. It is a simple matter of changing the type of exception thrown as response to SocketTimeoutExceptions.

E.g. in SoapConnection:

112
113
114
115
116
117
        } catch (SocketTimeoutException e) {
            long timeTaken = System.currentTimeMillis() - startTime;
            throw new RequestTimedOutException("Request to " + url + " timed out. TimeTaken=" + timeTaken +
                    " ConnectionTimeout=" + config.getConnectionTimeout() + " ReadTimeout=" + 
                    config.getReadTimeout(), e);
        }

And the RequestTimedOutException, e.g. at the bottom of SoapConnection:

    public static class RequestTimedOutException extends ConnectionException {
        private static final long serialVersionUID = 1L;
 
        private RequestTimedOutException(String message, Exception e) {
            super(message, e);
        }
    }

WSC issue 67.

Posted in Java, Salesforce | Tagged , , , | Leave a comment

Salesforce wsc hacking: removing compiler warnings

I hate compiler warnings. These little yellow warning signs in Eclipse annoys me. I will go far to avoid compiler warnings in my own code.

But what about generated code? Actually generated code is the worst as it does not make sense to correct the code to remove the warnings – they will reappear when the code is regenerated. And it demonstrates sloppiness in the code generation tool.

In this article I will work through and demonstrate how to remove the compiler warnings from the wsc generated code, and some of the infrastructure code as well.

Continue reading

Posted in Java, Salesforce | Tagged , , , | Leave a comment

Unit-testing time/timing dependent components

I’m often faced with developing components that are depending on time or timing, one way or the other.

An example can be a query that returns all “active” rows, given current date/time and where the rows have effective and expiry columns. Or components that report different state depending on time T, T+n, T+m etc.

Unit-testing components like these can be a nuisance, unless “current time” is part of the interface in the first place, and with some sleep() and stuff in the second situation. Not ideal.
Continue reading

Posted in Java | Tagged , , , , , , | Leave a comment

SimpleDateFormat instances are not thread-safe

It comes as a surprise to many developers that SimpleDateFormat instances are not thread-safe. Sometimes I encounter utility classes like below:

public class DateUtil {
	public static final SimpleDateFormat ISO_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
	public static final SimpleDateFormat SQL_TIMESTAMP_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
	// etc...
	public static Date parse(String date) throws ParseException {
		return ISO_DATE_FORMAT.parse(date);
	}
	public static String format(Date date) {
		return ISO_DATE_FORMAT.format(date);
	}
	// etc...
}

Looks innocent, but it will get you into trouble sooner or later if accessed by more than one thread at a time. And the problems are hard to find and debug, from strange dates in different application layers to strange exceptions where you’d expect none in a healthy system. And errors that only occurs very rare and more often when the system is under load…

Then what to do?
Continue reading

Posted in Java | Tagged | 5 Comments

Salesforce JAXB: Using wsimport to generate client artifacts

The Salesforce developer documentation provides JAVA examples based on WSC. WSC is a nice tool that makes interfacing to SFDC simple and easy, providing an EnterpriseConnection hiding all the details in calling SFDC.

But, WSC might not be for you:

  • Proprietary, not standards based
  • You might not be allowed to add third-party libraries to your application stack; or the process is too cumbersome…
  • Support, maintenance?

Unfortunately the wsimport tool that comes with the JDK does not “out of the box” like the Enterprise WSDL generated from Salesforce. Running it on the Enterprise WSDL from my test setup gives below output:

[jesper@linux3 ~]$ wsimport -keep enterprise.wsdl
parsing WSDL...
 
[WARNING] src-resolve: Cannot resolve the name 'tns:ID' to a(n) 'type definition' component.
  line 31 of file:/home/jesper/enterprise.wsdl#types?schema1
 
[ERROR] A class/interface with the same name "com.sforce.soap.enterprise.DescribeLayout" is already in use. Use a class customization to resolve this conflict.
  line 5952 of file:/home/jesper/enterprise.wsdl
 
[ERROR] (Relevant to above error) another "DescribeLayout" is generated from here.
  line 5620 of file:/home/jesper/enterprise.wsdl
 
[ERROR] Two declarations cause a collision in the ObjectFactory class.
  line 5952 of file:/home/jesper/enterprise.wsdl
 
[ERROR] (Related to above error) This is the other declaration.
  line 5620 of file:/home/jesper/enterprise.wsdl

Continue reading

Posted in Java, Salesforce | Tagged , , , | 2 Comments

Salesforce wsc: upsert null fields

 

The salesforce upsert() call  is brilliant for interfacing legacy systems with Salesforce, especially if the data you are going to replicate into already contains some sort of unique identification.

  • It will create records that does not exist, and update records that does exist, given the unique id.
  • It allows updating up to 200 records in one batch returning create-/update status for each individual item.
  • It’s fast – can easily push more than 100,000 records/hour, depending on object type.

Basically the upsert() operation is nice and easy to use; it will happily set or change almost any field in your Salesforce based CRM solution.

But, how do you null (or “blank”) previously set fields?

Continue reading

Posted in Java, Salesforce | Tagged , , , | Leave a comment

Salesforce wsc: ConnectionException should have been Fault

This is the first article where I discuss details or issues using wsc (JAVA) on an Oracle WebLogic platform to integrate Customer Legacy systems with Salesforce CRM.

In some environments (specifically the WLS 10.3.5 used as production environment at my current Customer) the wsc infrastructure throws a ConnectionException (com.sforce.ws.ConnectionException: Unexpected element) in situations where it should actually return a Fault (com.sforce.soap.enterprise.fault.ApiFault).

I have tried to mock a similar environment in order to demonstrate the problem to no avail.

But, I have discovered why the problem arises – it has to do with the infrastructure related to the Transport.isSuccessful().
Continue reading

Posted in Java, Salesforce | Tagged , , , , , | 2 Comments

usl4j – Ultra Simple Logging for JAVA

Back in the late 90’ties when I started programming JAVA there were no standard way of logging in JAVA programs.

One usually just used System.out.println(), System.err.println() and Exception.printStacktrace(). Often applications would build homemade logging frameworks that would also log information about timestamp etc, possibly logging to files and whatnot.

Later lots of different logging frameworks came about, e.g. JLog, log4j, commons-logging just to name a few. Larger application frameworks would supply their own logging framework; sometimes just repackaged versions of commons-logging or log4j.

When jdk 1.4 (a.k.a J2SE) was released, it contained a standard logging framework, and as far as I remember, this was actually based on the IBM Aphaworks developed JLog framework.

I thought that people would now stop using other third-party logging frameworks and just use jdk-logging, but no, a lot of people were angry that the JLog framework was chosen (and not log4j) and people would continue to use log4j. Other would use commons-logging, giving a simpler API and a “lightweight” solution towards not actually deciding on which logging framework to use.

Even another logging framework would see the day, because some people didn’t agree with neither jdk-logging, log4j or commons-logging and would still have the possibility to actually use either as the logging implementation: slf4j (Simple Logging Facade for Java (SLF4J)).

Personally, when I’m writing JAVA applications, I try to use as few third-party libraries as possible and use whatever the platform provides me. This in most cases means I will use jdk-logging as logging framework.
Continue reading

Posted in Java, Open Source | Tagged , | Leave a comment

Simple logarithm implementation in PIC assembler

Most natural observations are exponentational/logarithmic in nature.

I often do simple hardware using Microchip PIC processors (usually 12F or 16F families). Hardware doing some business in the home.

One such example is my Christmas-tree-lightning-controller. Basically it only turns my electric Christmas-tree-lights on when I want to see them on…

GPC

GPC

In order to do this, it needs to measure the out-door light. It uses an accumulation cell (of 8 samples) and a moving average register with 8 cells: Light or darkness is determined from 8 accumulated samples of “10-bits light” averaged through the latest 8×8 samples…

8×8 samples of 10-bits equals 6 bits of 10bits equals 16 bits…

So the output of the moving average register is at most 16 bits wide and should pose no problems on a modern microcontroller.

Unfortunately, the small PIC micros are not very good at “multi-byte” calculations why the 16-bit result is a bit awkward to deal with. And my simple experiments indicates that light-level comparisons should be measured on a “percentage change” basis and not as an absolute comparison.

If I could just do a simple 8-bit logarithm function of the 16-bit absolute result, I would be able to do simple percentage comparisons of different light level measurements by simple subtraction of 8-bit values. Even the simples PIC’s can do subtraction of 8-bit values without special engineered software subroutines 🙂

Being inspired by the µLaw compression schemes used in Telephony, I wrote this little routine, that basically calculates 16×log2(x), where x is some 16-bit value and the result is an 8-bit value. Simulating the calculations in software gives a maximum error of 1-bit on the result.

; NOTE: Variables wi, r0 and r1 are 
; defined elsewhere in on-chip RAM
;
; log2: calcuate r0 = approx 16*log2(r1:r0). 
; wi, r1 destroyed
;
log2:
        movlw   16
        movwf   wi
; loop
log2_loop:
        bcf     STATUS,C
        rlf     r0,f
        rlf     r1,f    ; C affected
        decfsz  wi,f    ; Z affected
        btfsc   STATUS,C
        goto    log2_done
        goto    log2_loop
; done
log2_done:
        movlw   0xf0
        andwf   r1,w
        iorwf   wi,w
        movwf   r0
        swapf   r0,f
        return

The simpe logarithm shown above I’ve used lots of different places. If You want to know how or why it works, don’t hesistate to put a comment below.

Unfortunately, my Christmas-tree-lights-controller is not (yet) perfect, as sudden, heavy light seems to overflow the moving averager. In other words, when the terrorist children living next-door are having fun with fireworks, the lights on my outdoor Christmas tree blinks in its own secret manner 🙂

Posted in Microcontoller, Open Source, Personal, PIC | Tagged , , | 2 Comments

Locking a row, portable between databases, with JDBC

A couple of years ago I was with a project designing a system to manage Investor Meetings.

An Investor must have a valid Pass in order to join a Meeting. Each Pass has a unique serial number.

It was a major issue and lots of things were tried to ensure that different Passes did not have the same number and that numbers were not “lost”. The system had to be “fast”, being able to create lots of Passes for different Meetings for different Organizations at the same time. Serializing transactions on central database tables was not an option.

For some reason, a simple “SELECT … FOR UPDATE” would not do the trick.

At one point it was decided to use database sequences. Each meeting would have a sequence for generating the Pass-numbers.

This proved to a bad idea too, as creating and destroying sequences are relatively slow operations and the system experienced deadlocks from time to time. Also, sequences are generally not rolled back when transactions are, and Pass-numbers would be “lost”.

I decided I would try to figure out why the simple “SELECT … FOR UPDATE” did not work and what database I could actually get it to work with.

This blog-entry is about my findings with different databases: DB2, Oracle, PostgreSQL, Apache Derby and HSQLDB.
Continue reading

Posted in Databases, Java | Tagged , , , , , | Leave a comment