Wednesday, May 08, 2013

Java synchronized methods: lock on object or class

http://stackoverflow.com/questions/437620/java-synchronized-methods-lock-on-object-or-class

In section 8.1.1 of the Java Language
Specification, third edition. The valid modifiers for a class are any
annotation, public, protected, private, abstract, static, final, or
strictfp. It is illegal to place the keyword "synchronized" in front of
a class.

The Java Tutorials say: "it is not possible for two invocations of synchronized methods on the same object to interleave." What does this mean for a static method? Since a static method has no associated object, will the synchronized keyword lock on the class, instead of the object?
Yes.

Synchronized method acquires a monitor (§17.1) before it executes. For a class (static) method, the monitor associated with the Class object for the method's class is used. For an instance method, the monitor associated with this (the object for which the method was invoked) is used.

One point you have to be careful about (several programmers generally fall in that trap) is that there is no link between synchronized static methods and sync'ed non static methods, ie:
class A {
static synchronized f() {...}
synchronized g() {...}
}

Main:

A a = new A();
Thread 1:

A.f();
Thread 2:

a.g();
f() and g() are not synchronized with each other and thus can execute totally concurrently.

Unless you implement g() as follows:
g() {
synchronized(getClass()) {
...
}
}
I find this pattern useful also when I want to implement mutual exclusion between different instances of the object (which is needed when accesing an external resource, for example).

SOA - Set Loopback for SOA suite installation on a DHCP host

In order to install SOA suite on a DHCP host, we need to set the loopback. If you have installed VM ware or Virtual Box, you are lucky and the loopback had been created. If not, just follow the follow steps to create a loopback.

(From http://docs.oracle.com/cd/E16764_01/install.1111/e14318/qisoa.htm#BGBJEFDH)

3.7 Installing on DHCP Hosts

If you are installing Oracle SOA Suite on a DHCP host, you must follow the configuration steps in this section for your platform.

3.7.1 For UNIX Platforms

On UNIX operating systems, configure the host to resolve host names to the loopback IP address by modifying the /etc/hosts file to contain the following entries:
127.0.0.1 hostname.domainname hostname
127.0.0.1 localhost.localdomain localhost
After doing so, check that the hostname resolves to the loopback IP address by entering the following command:
/bin/ping hostname.domainname

3.7.2 For Windows x86 Platforms

On Windows systems, install a loopback adapter on the DHCP server (see Section 3.7.3, "Installing a Loopback Adapter (Windows Only)"). This assigns a local IP address to your computer.
After installing the adapter, add a line to the %SYSTEMROOT%\system32\drivers\etc\hosts file with the following format, immediately after the localhost line:
IP_address   hostname.domainname   hostname
Replace IP_address with the local IP address of the loopback adapter.

3.7.3 Installing a Loopback Adapter (Windows Only)

To install a loopback adapter on Windows 2003 or Windows XP:
  1. Open the Windows Control Panel.
    Windows 2003: Select Start > Control Panel > Add Hardware.
    Windows XP: Select Start > Control Panel, then double-click Add Hardware.
  2. In the "Welcome" window, click Next.
  3. In the "Is the hardware connected?" window, select Yes, I have already connected the hardware, then click Next.
  4. In the "The following hardware is already installed on your computer" window, in the list of installed hardware, select Add a new hardware device, then click Next.
  5. In the "The wizard can help you install other hardware" window, select Install the hardware that I manually select from a list, then click Next.
  6. In the "From the list of hardware types, select the type of hardware you are installing" window, select Network adapters, then click Next.
  7. In the "Select Network Adapter" window, make the following selections:
    • Manufacturer: Microsoft
    • Network Adapter: Microsoft Loopback Adapter
  8. Click Next.
  9. In the "The wizard is ready to install your hardware" window, click Next.
  10. In the "Completing the Add Hardware Wizard" window, click Finish.
  11. If you are using Windows 2003, restart your computer.
  12. Right-click My Network Places on the desktop and choose Properties. This displays the Network Connections Control Panel.
  13. Right-click the connection that was just created. This is usually named "Local Area Connection 2". Choose Properties.
  14. On the "General" tab, select Internet Protocol (TCP/IP), then click Properties.
  15. In the "Properties" dialog box, click Use the following IP address and do the following:
    1. IP Address: Enter a non-routable IP for the loopback adapter. Oracle recommends the following non-routable addresses:
      192.168.x.x (x is any value between 1 and 255)
      10.10.10.10
      
    2. Subnet mask: Enter 255.255.255.0.
    3. Record the values you entered, which you will need later in this procedure.
    4. Leave all other fields empty.
    5. Click OK.
  16. In the "Local Area Connection 2 Properties" dialog, click OK.
  17. Close Network Connections.
  18. Restart the computer.

3.7.4 Removing a Loopback Adapter (Windows Only)

To remove a loopback adapter on Windows 2003 or Windows XP:
  1. Start the System Control panel.
    Windows 2003: Select Start > Control Panel > System.
    Windows XP: Select Start > Control Panel, then double-click System.
  2. In the "Hardware" tab, click Device Manager.
  3. In the "Device Manager" window, expand Network adapters. You should see Microsoft Loopback Adapter.
  4. Right-click Microsoft Loopback Adapter and select Uninstall.
  5. Click OK.

Oracle SOA Suite Basic

In SOA suite, we must understand the follow concepts.

  • Adapter - used to connect resources
  • Mediator - Route and Transform
  • Rule Engine - Business Rules
  • BPEL Process - Orchestration
  • Human Workflow - Involve human beings
  • Security Framework - Policy-based security
  • BAM - Business Activity Monitor, visualization of process running


What is SCA?

SCA - Service Component Architecture, which is a specification backed by a growing number of leading industry vendors (http://www.OSOA.org) and being standardized at OASIS, the international open standards consortium . SCA is:
  • language-neutral
  • component model 
  • assembly model
SCA terminologies:
  • Composite: deployment unit
  • Service: entry-point into composite
  • Component: provides logic
  • Reference: refers to external services
  • Wire: connects services, components and references – no special semantic.
SOA Composite:
  • SOA composite is a standards-based deployment unit
  • Leverages the SCA assembly model
  • Can mix variety of components: Mediator, BPEL, Human Workflow, etc.