Reflection Example (Part 5)

the main idea of this example is from one of the plural course from Mr. Jim Wilson.

Legacy Beans/Objects

Two beans of example, two types of BankAccount object.

BankAccount:


package com.navid.practice.reflection.legacy.beans;

/**
*
* @author Unknown_
*/
public class BankAccount {

private String accountNum;
private int balance;

public BankAccount(String accountNum, int balance) {
this.accountNum = accountNum;
this.balance = balance;
System.out.println("BankAccount Object constructor 2");
}

public BankAccount() {
System.out.println("BankAccount Object constructor 1 ");
}

public String getId() {
return this.accountNum;
}

public synchronized int getBalance() {
return this.balance;
}

public synchronized void deposit(int amount) {
balance += amount;
}

public synchronized void whithraw(int amount) {
balance -= amount;
}

}

 

HighVolumeBankAccount:


package com.navid.practice.reflection.legacy.beans;

/**
*
* @author Unknown_
*/
public class HighVolumeAccount extends BankAccount implements Runnable {

private int[] readDailyDeposit() {
System.out.println("HighVolumeAccount process all Daily Deposits.");
return null;
}

private int[] readDailyWithraw() {
System.out.println("HighVolumeAccount process all Daily Withrwals.");
return null;
}

@Override
public void run() {

this.readDailyDeposit();
this.readDailyWithraw();

}

}

 

 

Worker concepts:

WorkerThread is a good concepts, in this example we provide a simple form of threadworker nad then adding a better implemenation best practice to it to make the desing more robus and flexible and also loosely coupled.

AccountWorker


package com.navid.practice.reflection.newsrc.worker;

import com.navid.practice.reflection.legacy.beans.BankAccount;
import com.navid.practice.reflection.legacy.beans.HighVolumeAccount;

/**
*
* @author Unknown_
*/
public class AccountWorker implements Runnable {

private BankAccount bankAccount;
private HighVolumeAccount highVol;

public AccountWorker(BankAccount acc) {
this.bankAccount = acc;
}

public AccountWorker(HighVolumeAccount hva) {
this.highVol = hva;
}

/**
* do the actual work in here ...
*/
public void doWork() {

Thread thread;
thread = new Thread(this.highVol != null ? this.highVol : this);
//starting the thread instance in here.
thread.start();
}

@Override
public void run() {
// get this operation from BATCH file, or from other files, or from databases, or webservice, etc. any other none type related
//        //way to findout the operation.

System.out.println("Running Worker Thread in Run method - and target type is: " + (this.bankAccount == null ? this.highVol.getClass() : this.bankAccount.getClass()));
}

}

 

Testing the Scenario:

and for testing the application we can use below main class to trigger the whole process of worker thread.

package com.navid.practice.reflection.newsrc.worker;

package com.navid.practice.reflection.newsrc.worker;

import com.navid.practice.reflection.legacy.beans.BankAccount;
import com.navid.practice.reflection.legacy.beans.HighVolumeAccount;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
*
* @author Unknown_
*/
public class ReflectionPractice {

public void startWork(String workerTypeName, Object workerTarget) {

try {

// providing the worker thread information.
Class<?> workerType = Class.forName(workerTypeName);

// need to create a reference to the object that we have already
// in heap for BankAccount -- we need this to find proper constructor
Class<?> targetType = workerTarget.getClass();

//prepareing worker - specify what constructor you need
Constructor<?> workerConstructor = workerType.getConstructor(targetType);
// make an actual instance from worker class.
Object workerThread = workerConstructor.newInstance(workerTarget);
// get the proper methods to execute from worker
Method doWork_method = workerType.getMethod("doWork");

//then execute that method
doWork_method.invoke(workerThread);

} catch (ClassNotFoundException ex) {
Logger.getLogger(ReflectionPractice.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoSuchMethodException ex) {
Logger.getLogger(ReflectionPractice.class.getName()).log(Level.SEVERE, null, ex);
} catch (SecurityException ex) {
Logger.getLogger(ReflectionPractice.class.getName()).log(Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
Logger.getLogger(ReflectionPractice.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(ReflectionPractice.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalArgumentException ex) {
Logger.getLogger(ReflectionPractice.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvocationTargetException ex) {
Logger.getLogger(ReflectionPractice.class.getName()).log(Level.SEVERE, null, ex);
}

}

public static void main(String[] args) {

BankAccount bankAccount = new BankAccount("1234", 100);

new ReflectionPractice().startWork("com.navid.practice.reflection.newsrc.worker.AccountWorker", bankAccount);

HighVolumeAccount highVolumeAccount = new HighVolumeAccount();

new ReflectionPractice().startWork("com.navid.practice.reflection.newsrc.worker.AccountWorker", highVolumeAccount);
}

}

output

run:
BankAccount Object constructor 2
BankAccount Object constructor 1 
Running Worker Thread in Run method - and target type is: class com.navid.practice.reflection.legacy.beans.BankAccount
HighVolumeAccount process all Daily Deposits.
HighVolumeAccount process all Daily Withrwals.
BUILD SUCCESSFUL (total time: 0 seconds)

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s