Thursday 16 February 2012

soapUI

Tool for testing WebServices: soapUI. You can use it as a separate application (which is a paid version) or for free as an Eclipse plugin.
More info on eclipse version is here: http://www.soapui.org/IDE-Plugins/eclipse-plugin.html

Saturday 11 February 2012

Invoke webservice in java

To invoke WebService in java without any WS library, just using HttpURLConnection you can act like here:
   URL url = new URL(address);
   HttpURLConnection rc = (HttpURLConnection) url.openConnection();
   rc.setRequestMethod("POST");
   rc.setDoOutput(true);
   rc.setDoInput(true);
   rc.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
   int len = soapEnvelope.length();
   rc.setRequestProperty("Content-Length", Integer.toString(len));
   rc.setRequestProperty("SOAPAction", soapAction);
   rc.connect();
   OutputStreamWriter out = new OutputStreamWriter(
     rc.getOutputStream());
   out.write(soapEnvelope, 0, len);
   out.flush();
   InputStreamReader read = new InputStreamReader(rc.getInputStream());
   StringBuilder returns = new StringBuilder();
   int ch = read.read();
   while (ch != -1) {
    returns.append((char) ch);
    ch = read.read();
   }
   read.close();
   rc.disconnect();

ExecutorService

To easily run many Threads in java ExecutorService can be used. It is an abstraction for creating a pool of threads for running tasks. Tasks can return results in Future object. We can create Thread pools using methods of class Executors. There are plenty of options: amount of Threads to use, rise of ones,...
More details can be found on: http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/ExecutorService.html

An example of using these construction is here:
import java.util.Date;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ExecutorServiceExample {

 final int size = 20000000;

 private long[] arr = new long[size];

 class Task implements Callable {

  private int index, range;

  public Task(int index, int range) {
   this.index = index;
   this.range = range;
  }

  public Boolean call() throws Exception {
   boolean ret = false;

   for (int i = index; i < index + range; i++) {

   }

   return ret;
  }

 }

 public ExecutorServiceExample() {
  Random random = new Random(new Date().getTime());
  for (int i = 0; i < size; i++) {
   arr[i] = random.nextLong();
  }
 }

 public static void main(String[] args) {

  ExecutorServiceExample ese = new ExecutorServiceExample();
  ese.findSeq(123456789012345678l);
  ese.findConcur(123456789012345678l);

 }

 private boolean check(int i, long val) {
  if (arr[i] * val * val == val * val * val) {
   System.out.println("Found:" + i);
   return true;
  }
  return false;
 }

 private void findConcur(long val) {
  int range = 100;
  long start = new Date().getTime();
  ExecutorService service = Executors.newFixedThreadPool(100);

  for (int i = 0; i < size / range; i++) {
   service.submit(new Task(i * range, range));
  }
  service.shutdown();
  System.out.println("Seq in " + (new Date().getTime() - start));
 }

 private void findSeq(long val) {
  long start = new Date().getTime();
  for (int i = 0; i < size; i++) {
   check(i, val);
  }
  System.out.println("Seq in " + (new Date().getTime() - start));
 }
}

To run this example you will probably need to add parameter to increase heap size (f.e. -Xmx1300m)

Thursday 9 February 2012

Exposing variables by JMX in Spring Applications

If you wish to expose variable by JMX in the Spring application you have to:
  1. Add in the spring context the tag:
    <context:mbean-export/>
  2. On the managed class add annotation @ManagedResource
  3. On this method you want to be exposed add annotation
    • For getters and setters - @ManagedAttribute
    • For all type of operations - @ManagedOperation

Multitenancy

Multitenancy refers to a principle in software architecture where a single instance of the software runs on a server, serving multiple client organizations (tenants). Multitenancy is contrasted with a multi-instance architecture where separate software instances (or hardware systems) are set up for different client organizations. With a multitenant architecture, a software application is designed to virtually partition its data and configuration, and each client organization works with a customized virtual application instance.

Sunday 5 February 2012

Adding Google Reader subscribe button

If you want to add in the webpage the button to subscribe RSS by Google Reader, then use this page: http://www.google.com/webmasters/add.html. There you can put the url to your RSS in one field and generate the HTML code needed to be inserted into the web page content. That's all!

How to escape HTML?

To escape HTML for usage in blogs, web pages, ... you can use: http://www.blogcrowds.com/resources/parse_html.php

Saturday 4 February 2012

How to highlight code syntax in the blogger? Use SyntaxHighlighter!

I decided to use a library for highlighting code syntax. I have found SyntaxHighlighter. To install it you have to add some script in the html code. For my case I wanted to use it for java and xml code.
Here is what I have added (you can use hosted files or files from your server):
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJava.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushXml.js' type='text/javascript'/>

<link href='http://alexgorbatchev.com/pub/sh/current/styles/shCore.css' rel='stylesheet' type='text/css'/>
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css ' rel='stylesheet' type='text/css'/>
<script type='text/javascript'>
     SyntaxHighlighter.all()
</script>
Then you just need to put your code inside pre tag:
<pre class="brush: java">
  int add(int a, int b) {
     return a + b;
  }
</pre>
And you will get:
  int add(int a, int b) {
     return a + b;
  }
More info on the SyntaxHighlighter home page: http://alexgorbatchev.com/SyntaxHighlighter/
More on installation is described on: http://alexgorbatchev.com/SyntaxHighlighter/manual/installation.html
List of languages whose syntax are implemented are here: http://alexgorbatchev.com/SyntaxHighlighter/manual/brushes/

Thursday 2 February 2012

Lucidchart diagrams

If you are in need to create a diagram and you have no tool for it, try http://www.lucidchart.com/. Your browser is enough. You can draw Flowcharts, UML Diagrams, Mid Maps, Organisation Diagrams and others.