Faking a connection refusal


Your application connects to another server to request a service.  Most of the time this hums along with no problem.  But once in a while the server you are attempting to connect to refuses the connection.  When this happens, the phones ring off the hook.

You would like to catch this scenario before all your users get angry.  So you put some monitoring in place.

To test the monitoring, you need to fake the connection refusal.  This took me longer than I care to admit to figure out, but it turns out the answer is pretty simple.

Change the port number.

So say you have Axis2 software running at the port number you are connecting to.  Keep your connection string as it is, except for the port number.  Change the port to a number that is not running any web service software.

That will result in a java.net.ConnectException: Connection refused error.




Learning Python – make a URL request

rosetwoandbuds002  Been learning Python lately.  Have done a couple of tutorials and one or two simple programs.  This post will be about a simple example of how to access some real estate information that is available from a company called Trulia.

First, some links to sites that helped me figure this out:

  • Download Python, install, and some reference documentation: https://www.python.org/downloads/
  • Stackoverflow thread about making a URL request in Python: http://stackoverflow.com/questions/17301938/making-a-request-to-a-restful-api-using-python
  • How to make a Trulia RSS feed with your real estate search criteria: https://www.trulia.com/tools/rss/#code_block
  • Some free python IDEs: http://insights.dice.com/2014/09/23/look-5-free-python-editors/
  • I decided to go with Visual Studio for an IDE. I picked the custom installation so I could specify Python as a supported language.  https://www.visualstudio.com/downloads/

The code:

#Just a little POC to retrieve some real estate information from the web
#Python 3
import urllib.request #for opening connections to URLs

def main():

  truliaURL = 'https://www.trulia.com/rss2/for_sale/Burbank,CA/'

  request = urllib.request.Request(truliaURL)
  response = urllib.request.urlopen(request)
  responseData = response.read().decode('utf-8')

if __name__ == "__main__":

Debugging BlazeDS Performance

raindropsonrosebud I have a server call that returns a lot of data to the client.  About 80% of the elapsed time for the call is the time after the data leaves the server and before it arrives at the client.

We’re using BlazeDS to communicate between a Flex client and a Java back end.

It is crazy hard to find much information on performance tuning for BlazeDS.  But these two blog posts helped me figure out how to get some statistics about the messaging between server and client – things like elapsed time, size of message, etc.

So basically what you do is, on the server side in your services-config.xml file, in the tag of your tag, you add these two tags:


Then on the client side, when you instantiate a new AMFChannel, you add a line like this:


And add a new function called customSettings which sets the values for these same properties on the client-side.

 private function customSettings():XML {
 return <channel-definition>

Doing it this way gets around the problem of them being read-only properties.

Now, upon successful completion of service calls, add this code to your result handler:

log.debug(getResource(msg, destination, operationName, elapsedTime)); var performanceDetails:MessagePerformanceUtils = new MessagePerformanceUtils(event.message); log.debug(performanceDetails.prettyPrint());

More information on MessagePerformanceUtils here: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/messaging/messages/MessagePerformanceUtils.html#methodSummary

What this does is get you a bunch of information to help with figuring out why the call is taking so long.

Original message size(B): 1685
Response message size(B): 6987918
Total time (s): 8.402
Network Roundtrip time (s): 3.584
Server processing time (s): 4.818
Server adapter time (s): 3.708
Server non-adapter time (s): 1.11

Don’t leave this in your production code.  You’re trying to improve performance, not make it worse!

Hibernate ProjectionList and ResultTransformer to solve problem of massive queries with endless joins

Hibernate ProjectionList and ResultTransformer to solve problem of massive queries with endless joins

gardenoftwedenprofileHibernate can make query building and entity mapping easy, but if you let it take too much control you can also end up with huge queries that create a big performance drag.

If you know you only need data from a few specific columns of your table or tables, you can use a ProjectionList to target just those columns, and use a ResultTransformer to form the raw results into the sparsely populated entity.

To illustrate the point, let’s take an example of an invoice and its line items.  We have a one-to-many relationship between the invoice table and the line item table.  From the invoice table, we want the invoice_id, invoice_number, invoice_date columns.  The invoice table is linked to a vendor table, and from that table we want the vendor_name column.  From the invoice line items, we want the line_item_number, amount and description columns.

Criteria criteria = session.createCriteria(invoice.class);
criteria.createAlias("vendor", "v");
criteria.createAlias("lineItems", "li");

You need to specify aliases for “vendor” and “lineItems” in order to be able to specify the properties from those related entities.  If you had a situation where there wasn’t always a vendor, but you wanted information from invoices that didn’t have vendors, you would specify the alias like this:

criteria.createAlias("vendor", "v", Criteria.LEFT_JOIN);

Since you have a one-to-many relationship from invoice to lineItem, you will get a separate object from this query for every line item.  You will get the same invoice information repeated, with different line item information.  In other words, if you have an invoice that has two line items in it, the raw data returned from the query will look like this:

o = {java.lang.Object[7]}
[0] = 1234   -- the internal ID for the invoice
[1] = "LAP-12355" -- the invoice number
[2] = "12/31/2016" -- the invoice date
[3] = "George's Great Grill" -- the vendor name
[4] = "1" -- the invoice line item number
[5] = 400.23 -- the invoice line item amount
[6] = "ribs" -- the invoice line item description

And then you might have a second object returned with the same information exactly in elements 0 through 3, but with the following for elements 4, 5, and 6:

[4] = "2" -- the invoice line item number
[5] = 20.00 -- the invoice line item amount
[6] = "delivery charge" -- the invoice line item description

This is where the ResultTransformer comes in.  The ResultTransformer is a method that transforms the raw results returned from the SQL query into the entity you use in your Java code.  There are built-in ResultTransformers you can use.  For this example we will write our own, to illustrate how it works.

Specify the ResultTransformer on your criteria like this:

criteria.setResultTransformer(new ResultTransformer()
    public Object transformTuple(Object[] o, String[] strings)
        return transformObjectToInvoice(o);

    public List transformList(List list)
        return consolidateInvoices(list);

Then you write a private method transformObjectToInvoice that takes an Object[] and returns an Invoice.  Every invoice it will return will have one line item.  And you write a private method consolidateInvoices that takes a List and returns a List. But the incoming list will have invoices with only one line item, and the outgoing list will have fewer invoices, and the invoices will have 1 to n line items apiece.

So your  transformObjectToInvoice will look something like this:

private Invoice transformObjectToInvoice(Object[] o)
    Invoice invoice = new Invoice();
    invoice.setId((Integer) o[0]);
    invoice.setInvoiceNumber((String) o[1]);
    return invoice;

And your consolidateInvoices will look something like this:

private List consolidateInvoices(List list)
    List consolidatedInvoices = new ArrayList<~>();
    Map<Integer, Invoice> invoices = new HashMap<~>();
    For (Invoice oneLineItemInvoice : list)
        Invoice mapInv = invoices.get(oneLineItemInvoice.getId());
        if (mapInv != null)
            invoices.put(oneLineItemInvoice.getId(), oneLineItemInvoice);
    return consolidatedInvoices;