Posts

Showing posts from December, 2011

Java Ping Servers - Flyweight Reduce Memory Footprint

package ServerProbe;

/*
 * Simple demonstration how to use the flyweight design pattern to improve performance and reduce
 * memory footprint.
 * Create a ServerFactory based on a pre-defined number of server types e.g. Windows, Unix etc
 *  Within Server object, for example have a shareable method such as pingIT, also the server_type is
 * shareable
 * Pass IP address and ping duration in as parameters
 *
 */

public class Server {
  String server_type;

  Server(String type){
  server_type = type;
  }

  String getType() {
     return server_type;
  }

  public void pingIT(String ip, int duration) {
  System.out.println("Pinging ->" + server_type + " IP -> " + ip);
  }

  public String toString() {
      return server_type;
  }
}

import java.util.HashMap;

public class ServerFactory  {
  private static final HashMap serverByType = new HashMap();

  public static Server getServer(String serverType) {
     Server server  = (Server) serverByType.get(serverType);

   …

Composite Design-Pattern Amazon Payment Lines

package PaymentManager;

public class Order {
  String reference;
  double value;

  Order(String r, double v){
      reference = r;
      value = v;
  }
  String getReference() {
     return reference;
  }
  double getValue() {
     return value;
  }
  public String toString() {
      return reference;
  }
}


public class Payment {
  Payment pay;
  Order[] orders;
  String order_type;
  Payment(Payment p, Order[] o, String type ) {
      this(o, type);
      this.pay = p;
  }

  Payment(Order[] o, String type) {
      orders = o;
      order_type =type;
  }
  String getOrderType() {
      return order_type;
  }
  Payment getPayment() {
      return pay;
  }
  Order[] getOrders() {
      return orders;
  }
  public String toString() {
      return "Order Type -> " +order_type;
  }
}


public class OrderTestRun {
  public static void main(String[] args) {
  Order[] detail = {
  new Order("AMZ-SUB-001", 15.00),
  new Order("AMZ-SUB-002", 15.00)
      };
      Pa…

Using Amazon SQS from Java

You can find the AWS SDK for Java API Reference - 1.2.15 here
http://docs.amazonwebservices.com/AWSJavaSDK/latest/javadoc/overview-summary.html
....
....
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.sqs.AmazonSQS;
import com.amazonaws.services.sqs.AmazonSQSClient;
....
.... static public String awsAccessKeyId = "";

static public String awsSecretAccessKey = "";
....
....
AmazonSQS sqs =
    new AmazonSQSClient(new BasicAWSCredentials(awsAccessKeyId ,awsSecretAccessKey ));

....
....

String url =
    sqs.createQueue(new CreateQueueRequest("dtm_test_queue")).getQueueUrl();
....
....

sqs.sendMessage(new SendMessageRequest(url, "Queue Data"));

Demo file copy using Guava

/* Demo file copy using Guava
 * src:file to be copied
 * dst:copied/new file created from copy.
 */
   public void dtmCopyFile(final String src, final String dst)
   {
      checkNotNull(src, "Source file cannot be null.");

      checkNotNull(dst, "Destination file cannot be null.");

      final File srcFile = new File(src);
      final File dstFile = new File(dst);

      try
      {
         Files.copy(srcFile, dstFile);
      }
      catch (IOException fileIoEx)
      {
         err.println("ERROR");
         err.println("Error copying file.");
err.println("Error Message: " + fileIoEx.toString() );
      }
   }

EJB Intercepts

EJB3 offers the ability to intercept EJB business methods by using AroundInvoke interceptors.

From this you can access the method name, the parameters passed, and the EJB context.

This is useful for

1. Performing security checks
2. Performing audits
3. Changing the values of method parameters in your Java EE applications using EJB interceptors

Here is a simple EJB interceptor:

public class InterceptorHandler {

@Resource
 private EJBContext context;

@AroundInvoke
protected Object audit(InvocationContext ctx) throws Exception {
    Principal principal = context.getCallerPrincipal();
    if (userIsValid(principal)) {
  // Complete code for logging
      }else{
  // Complete code for logging and exception handling
    }
    return ctx.proceed();
  }
}
To use this interceptor put an annotation on the designated EJB, for example

@Interceptors(InterceptorHandler.class)
@Stateless
public class TestBean {
  // Complete code here
}

ActiveMQ, easy to use open source message oriented middleware (MOM)

ActiveMQ is message oriented middleware (MOM), useful for receiving and processing asynchronous messages (queues or topics).
It is easily integrated with Java based applications (and others) via  the JMS API.
ActiveQ offers enterprise scale features which may be required when implementing complex distributred systems such as High availability with failover.Scalability and clustering through a distributed Network of Brokers.Pluggable persistence stores (JDBC, BDB, JDBM, file system).Distributed destinations and XA support.Caching.Connection pooling.Support for cross language clients such as .NET, C++.Support for scripting environments such as Perl, Python, and Ruby.Multiple Transport layers (TCP, UDP, multicast, NIO, SSL, Zeroconf, JXTA, JGroups).

Download at http://activemq.apache.org/

Getting to grips with Google Analytics API

Getting to grips with Google Analytics API


This shows the top 15 regions / by source / by number of visits decending

<?php

require 'gapi.class.php';

$username='your gmail email address';
$password='your password';
$profile_id='your profile ID, you can get this from the url when you view the standard report. Just look for id on the url';

$ga = new gapi($username,$password);

$ga->requestReportData($profile_id,array('source','region'),array('pageviews','visits'), '-visits','',
 '2011-11-01', // Start Date
                       '2011-11-30', // End Date
                       1,  // Start Index
                       15 // Max results
                       );

echo '<strong>Top 20 Region/Source by Visits</strong><br><br>';
echo '<table><tr>';

echo '<td>Region</td><td>Source</td><td>Page Views</td>…

AWS SDK for .NET with ElasticCache Support

AWS launched Amazon ElastiCache in Ireland

AWS launched Amazon ElastiCache in the US East (Northern Virginia) Region back in August and today enabled ElastiCache in EU West (Dublin), US West (Northern California), and Asia Pacific (Singapore and Tokyo).

New X-CART Store

A great site and resource if you require x-cart specific add-ons.  They developt e-commerce modules and templates for the x-cart platform.  I've worked with several of the modules and all are very easy to implement.  In addition if you don't want the hassle of installing the components yourself, William Hayes and his team will be more then happy to help

Check it out now The XCART Store

Top 8 Performance Problems on the Top 50 Retail Sites before we enter Holiday Seaso

Here are the top 5 performance bottlenecks that online retailers face.

Problem #1: Too many Redirects result in delayed First Impression
Problem #2: Web 2.0 / JavaScript impacting onLoad and blocking the browser
Problem #3: Complex CSS Selectors failing on IE8
Problem #4: 3rd Party Plugins such as Superfish
Problem #5: Large Content leads to long load times

Check out the full article here