2018-07-23

Java 9 Reactive Streams

JDK 1.9 :  Reactive Streams

The Reactive Streams initiative provides a standard for asynchronous stream processing with nonblocking back pressure. A reactive stream provides a way to signal its source to ease production of data when the stream's destination becomes overwhelmed with that data. This signaling capability is like a valve on a water pipe. Closing this valve increases back pressure (the pressure back at the source) while easing the burden on the destination.

The initiative's objective is to govern the exchange of stream data across an asynchronous boundary (such as passing data to another thread) while ensuring that the destination isn't forced to buffer arbitrary amounts of data. In other words, back pressure is an integral part of this model in order to allow the queues that mediate between threads to be bounded. Note that the communication of back pressure is handled asynchronously.

Reactive Streams focuses on finding a minimal set of interfaces, methods, and protocols for describing the operations and entities needed to achieve the objective: asynchronous streams of data with nonblocking back pressure.

More details



What is double locking java

Java programmers created the double-checked locking to be used with the Singleton creation pattern to limit how much code is synchronized. However, due to some little-known details of the Java memory model, this double-checked locking is not guaranteed to work. Instead of failing consistently, it will fail sporadically.
In addition, the reasons for its failure are not obvious and involve intimate details of the Java memory model. These facts make a code failure due to double-checked locking very difficult to track down.

Singleton creation:

 To understand where the double-checked locking originated, you must understand the common singleton creation.

  1. class Singleton {
                   private static Singleton instance;
  2.               private Singleton() {
  3.                    } 
  4.                public static Singleton getInstance() { 
  5.                  if (instance == null)
  6.                      instance = new   Singleton();
  7.                  return instance;  
  8.                } 
  9.             } 
  10.  
The design of this class ensures that only one Singleton object is ever created. The constructor is declared private and the getInstance() method creates only one object. This implementation is fine for a single-threaded program. However, when multiple threads are introduced, you must protect the getInstance() method through synchronization. If the getInstance() method is not protected, it is possible to return two different instances of the Singleton object.

If multiple thread access getInstance()  methid, The result is that the getInstance() method created two Singleton objects when it was supposed to create only one. This problem is corrected by synchronizing the getInstance() method to allow only one thread to execute the code at a time.

  1. public static synchronized Singleton getInstance() { 
  2.  if (instance == null) //1 
  3.    instance = new Singleton(); //2
     return instance; //3
  4. }
The code in Listing works fine for multithreaded access to the getInstance() method. However, when you analyze it you realize that synchronization is required only for the first invocation of the method. Subsequent invocations do not require synchronization because the first invocation is the only invocation that executes the code, which is the only line that requires synchronization. All other invocations determine that instance is non-null and return it. Multiple threads can safely execute concurrently on all invocations except the first. However, because the method is synchronized, you pay the cost of synchronization for every invocation of the method, even though it is only required on the first invocation.

In an effort to make this method more efficient, an idiom called double-checked locking was created. The idea is to avoid the costly synchronization for all invocations of the method except the first. The cost of synchronization differs from JVM to JVM. In the early days, the cost could be quite high. As more advanced JVMs have emerged, the cost of synchronization has decreased, but there is still a performance penalty for entering and leaving a synchronized method or block. Regardless of the advancements in JVM technology, programmers never want to waste processing time unnecessarily.

Because only line //2 in Listing 2 requires synchronization, we could just wrap it in a synchronized block

2018-07-16

How do you write an algorithm for the multiplication of two matrix [2D array]?

Problem statement:
Given two matrices, the task is to multiply them. Matrices can either be square or rectangular.
Example:
Input: int m1[][] = {{2, 3, 4}, {5, 6, 7}}; // 2 X 3 matrix
int m2[][] = {{1, 2}, {3, 4}, {5, 6}}; // 3 X 2 matrix
Output: {{31, 40},{58,76}}
  1. public class MatrixMultiplication {
  2.     public static void main(String args[]) {
  3.         int m1[][] = {{2, 3, 4}, {5, 6, 7}};    // 2 X 3 matrix
  4.         int m2[][] = {{1, 2}, {3, 4}, {5, 6}};  // 3 X 2 matrix
  5.         int res[][] = new int[2][2];    // resultant matrix of 2 X 2
  6.         for (int i = 0; i < 2; i++) {   // i represent row
  7.             for (int j = 0; j < 2; j++) {   // j represent column
  8.                 res[i][j] = 0;  // assume res[0][0] = 0
  9.                 for (int k = 0; k < 3; k++) {
  10.                     // k represent first matrix i.e. m1 -> number of column for                        // counter sum
  11.                     res[i][j] = res[i][j] + m1[i][k] * m2[k][j];
  12.                 }
  13.             }
  14.         }
  15.         // printing the resultant matrix
  16.         for (int i = 0; i < 2; i++) {
  17.             for (int j = 0; j < 2; j++) {
  18.                 System.out.print(res[i][j] + " ");
  19.             }
  20.             System.out.println();
  21.         }
  22.     }
  23. }
Method-II:
  1. public class MatrixMultiplication2D {
  2.     public static void main(String args[]) {
  3.         int m1[][] = {{2, 3, 4}, {5, 6, 7}};    // 2 X 3 matrix
  4.         int m2[][] = {{1, 2}, {3, 4}, {5, 6}};  // 3 X 2 matrix
  5.         int sum = 0;    // initial sum value is 0
  6.         int res[][] = new int[2][2];    // resultant matrix of 2 X 2
  7.         for (int i = 0; i < 2; i++) {   // i represent row
  8.             for (int j = 0; j < 2; j++) {   // j represent column
  9.                 res[i][j] = 0;  // assume res[0][0] = 0
  10.                 for (int k = 0; k < 3; k++) {
  11.                     // k represent first matrix i.e. m1 -> number of column for                        // counter sum
  12.                     sum = sum + m1[i][k] * m2[k][j];
  13.                 }
  14.                 res[i][j] = sum;    //  assigned sum value to resultant matrix
  15.                 sum = 0;    // reset to 0 for next iteration
  16.             }
  17.         }
  18.         // printing the resultant matrix
  19.         for (int i = 0; i < 2; i++) {
  20.             for (int j = 0; j < 2; j++) {
  21.                 System.out.print(res[i][j] + " ");
  22.             }
  23.             System.out.println();
  24.         }
  25.     }
  26. }
Output:
31 40
58 76

2018-07-08

RTC plugin component Developments

See Env setup

Create Plug-in 

Open IDE(Eclipse)
Create a new Plug-in Development->Plug-in Project
Fill out the second dialog as follows, and select Next.


































Fill out the second dialog as follows, and select Finish.

Open MANIFEST.MF or Plugin.xml file for your project

From the Overview tab select This plug-in is a singleton. Save this file.

From the Dependencies tab, select the Add… button. Add the following four plug-ins to the list. After adding, save the file

com.ibm.team.process.common 
com.ibm.team.process.service 
com.ibm.team.workitem.common 
com.ibm.team.repository.common

these are the few dependencies, for others functionality you may have to add others dependencies.
Like for 
SCM you have to add
scm.common and * scm.service
repository.common and repository.service
Search for available .service and .common and add it


Adding Extensions 

Open MANIFEST.MF or Plugin.xml file for your project
GO to Extensions Tab

search for component 
extension point="com.ibm.team.repository.common.components" and add
Enter component id and Name, then Save

It will auto generate Plugin.xml 
  <?xml version="1.0" encoding="UTF-8" ?>
  <?eclipse version="3.2"?>
  <plugin>
  <extension point="com.ibm.team.repository.common.components">
  <component id="com.test.ext.extensions" name="RTC Extensions" />
  </extension>

  </plugin>

Create two interface inside src

public interface IChangeDefinitions {
public static String EXTENSION_ID = "com.rtcext.extensions.service.OnChange";

}
public interface IComponentDefinitions {

public static String COMPONENT_ID = "com.test.ext.extensions";
}

and save it.

Your component is created.

Remember component ID 



Download Template
More Info From IBM
Other Example

RTC server side Plugin Extension Deployment


RTC server side UI plugin developments

RTC precondition plugin developments

Precondition actions are events that are generated before any operation/event is trigger.
A Advisors is basically operational behavior that happens before a certain operation completed.

Extension point = com.ibm.team.process.service.operationAdvisors
Implement the interface com.ibm.team.process.common.advice.runtime.IOperationAdvisor


You have to create two plugin project 
1. Component  
2. Service

Link for Component

Service Development 

Create Plug-in 

Open IDE(Eclipse)
Create a new Plug-in Development->Plug-in Project
Fill out the second dialog as follows, and select Next.


































Fill out the second dialog as follows, and select Finish.

Open MANIFEST.MF or Plugin.xml file for your project

From the Overview tab select This plug-in is a singleton. Save this file.

From the Dependencies tab, select the Add… button. Add the following four plug-ins to the list. After adding, save the file

com.ibm.team.process.common 
com.ibm.team.process.service 
com.ibm.team.workitem.common 
com.ibm.team.repository.common

these are the few dependencies, for others functionality you may have to add others dependencies.
Like for 
SCM you have to add
scm.common and * scm.service
repository.common and repository.service
Search for available .service and .common and add it

Adding Extensions 

Open MANIFEST.MF or Plugin.xml file for your project
GO to Extensions Tab

Search for operationAdvisors 
Select com.ibm.team.process.service.operationAdvisors

Under the Extension Element Details section. The operationId entry indicates which RTC operation we intend this plug-in to augment. When finished, save the file. 

List of operation Id
Or you can get operation id from process template

Field
Value
id
com.test.rtc.ext.id
name
Any Name
operationId
com.ibm.team.workitem.operation.workItemSave


Download Template
More Info From IBM
Other Example

RTC follow-up action plugin development

Follow-up actions are events that are generated when the operation is completed.
A Participant is basically operational behavior that happens after a certain operation completed.

Extension point = com.ibm.team.process.service.operationParticipants
Implement the interface com.ibm.team.process.common.advice.runtime.IOperationParticipant


You have to create two plugin project 
1. Component  
2. Service

Link for Component

Service Development 

Create Plug-in 

Open IDE(Eclipse)
Create a new Plug-in Development->Plug-in Project
Fill out the second dialog as follows, and select Next.


































Fill out the second dialog as follows, and select Finish.

Open MANIFEST.MF or Plugin.xml file for your project

From the Overview tab select This plug-in is a singleton. Save this file.

From the Dependencies tab, select the Add… button. Add the following four plug-ins to the list. After adding, save the file

com.ibm.team.process.common 
com.ibm.team.process.service 
com.ibm.team.workitem.common 
com.ibm.team.repository.common

these are the few dependencies, for others functionality you may have to add others dependencies.
Like for 
SCM you have to add
* scm.common and * scm.service
repository.common and repository.service
Search for available .service and .common and add it


Adding Extensions 

Open MANIFEST.MF or Plugin.xml file for your project
GO to Extensions Tab

Search for operationParticipants 
Select com.ibm.team.process.service.operationParticipants

Under the Extension Element Details section. The operationId entry indicates which RTC operation we intend this plug-in to augment. When finished, save the file. 

List of operation Id
Or you can get operation id from process template

Field
Value
id
com.test.rtc.ext.id
name
Any Name
operationId
com.ibm.team.workitem.operation.workItemSave




Download Template
More Info From IBM
Other Example

RTC java Server side plugin developments Environment setup

List of jar required to setup development evn.

link for documents and jars
https://jazz.net/downloads/rational-team-concert/releases/6.0.5?p=allDownloads 

From  Web Installers
Download IDE as per your OS. Fow Win Download 

From IBM Installation Manager Repositories
Download Server Link

From Source Code
Download Client SDK
Download Server SDK

This Two are Importants.

Once Download is complete Extract all files.

Step for Developments 

Open IDE 
Goto Windows Preferences 
search "target", select Platform target.

In target Add Server SDK and Client SDK.





RTC extension plugin developments

This documents will help you to creation and enhancement of an RTC custom extension. You can create RTC server side plugin.

1. Setup of your development environment

2. RTC follow-up action development

3. RTC precondition developments

4. RTC server side UI plugin developments

5. RTC server side Plugin Extension Deployment