Question
Marsh
Marsh
US
Marsh
Posted: Aug 21, 2015
Last activity: Aug 26, 2020
Last activity: 26 Aug 2020 8:17 EDT
Closed
Solved
Singelton pattern and Java connector
Can we use java connector if the service is in singleton pattern? Also, how do we use it?
Hi Apoorva,
Singleton design pattern is to ensure one instance of the class per object in the JVM. In my code example below i use Private static variable of the same class that is the only instance of the class.
and a Public static method that returns the instance of the class, this is the global access point for outer world to get the instance of the singleton class.
You can use Connect-Java for this, but i have implemented this example importing my custom JAR containing singleton method to pr_engineclasses table:
Code:
*********************************************************************************************************************************************
package com.pega.pegarules;
public class SingletonExample {
private static SingletonExample instance = null;
public String productVersion;
private SingletonExample() {
this.productVersion = "Pega 7";
}
public static SingletonExample getInstance() {
if (instance == null) {
synchronized (SingletonExample.class) {
if (instance == null) {
instance = new SingletonExample();
}
}
}
return instance;
}
public String getProductVersion(){
return this.productVersion;
}
public static void main(String args[]){
SingletonExample instance = SingletonExample.getInstance();
Hi Apoorva,
Singleton design pattern is to ensure one instance of the class per object in the JVM. In my code example below i use Private static variable of the same class that is the only instance of the class.
and a Public static method that returns the instance of the class, this is the global access point for outer world to get the instance of the singleton class.
You can use Connect-Java for this, but i have implemented this example importing my custom JAR containing singleton method to pr_engineclasses table:
Code:
*********************************************************************************************************************************************
package com.pega.pegarules;
public class SingletonExample {
private static SingletonExample instance = null;
public String productVersion;
private SingletonExample() {
this.productVersion = "Pega 7";
}
public static SingletonExample getInstance() {
if (instance == null) {
synchronized (SingletonExample.class) {
if (instance == null) {
instance = new SingletonExample();
}
}
}
return instance;
}
public String getProductVersion(){
return this.productVersion;
}
public static void main(String args[]){
SingletonExample instance = SingletonExample.getInstance();
System.out.println(instance.getProductVersion());
}
}
*********************************************************************************************************************************************
Inside my activity Java step singleton class is loaded, SingletonExample class is not loaded into memory and only when someone calls the getInstance method, this class gets loaded and creates the Singleton class instance.
Output in the PegaRULES log:
-Adi