https://medium.com/@gayan.dissanayake/using-helm-chart-in-spring-boot-application-daf178a9fd3f
WelCome to Gayan's Blog
Tuesday, September 3, 2024
Monday, September 2, 2024
Sunday, September 1, 2024
Thursday, March 24, 2016
Employee salary calculation using Drool
before start this example you need to have basic understanding about Drools and this example shows how to calculate salary based on different business logic if you have any issues regarding this example you can contact me.
following component are used to calculate salary
- basic Salary
- vehicle Rental Allowance
- traveling Allowance
- tax Percentage
- salary Amount
- SKU_H("daily", "Daily Workers")
- SKU_B("monthly", "Monthly paid workers")
- BHL("contract", "Contract worker")
and .drl files
my example i have integrate Drools with Spring please refer drools-context.xml
need to import this xml though applicationContext.xml file as follows
basic_salary.drl
vehicleRental.drl
tax.drl
salary_calculation.drl
please find maven dependence details below
/**
* Created with IntelliJ IDEA.
* User: Gayan Dissanayake
* Date: 02.11.15
*/
public enum EmployeeType {
SKU_H("daily", "Daily Workers"),
SKU_B("monthly", "Monthly paid workers"),
BHL("contract", "Contract worker");
private final String type;
private final String description;
private EmployeeType(String type, String description) {
this.type = type;
this.description = description;
}
public String getType() {
return type;
}
public String getDescription() {
return description;
}
}
Employee.java
package com.goodhope.fas.drools.model;
import com.goodhope.fas.drools.enums.EmployeeType;
/**
* Created with IntelliJ IDEA.
* User: Gayan Dissanayake
* Date: 02.11.15
*/
public class Employee {
private final EmployeeType employeeType;
private String employeeName;
private String employeeTypeDescription;
private double baseSalary;
private double vehicleRental;
private double travellingAllowance;
private double taxPercentage;
private double salaryAmount;
public Employee(EmployeeType employeeType) {
this.employeeType = employeeType;
employeeTypeDescription = employeeType.getDescription();
}
public EmployeeType getEmployeeType() {
return employeeType;
}
public String getEmployeeName() {
return employeeName;
}
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
public String getEmployeeTypeDescription() {
return employeeTypeDescription;
}
public void setEmployeeTypeDescription(String employeeTypeDescription) {
this.employeeTypeDescription = employeeTypeDescription;
}
public double getBaseSalary() {
return baseSalary;
}
public void setBaseSalary(double baseSalary) {
this.baseSalary = baseSalary;
}
public double getVehicleRental() {
return vehicleRental;
}
public void setVehicleRental(double vehicleRental) {
this.vehicleRental = vehicleRental;
}
public double getTravellingAllowance() {
return travellingAllowance;
}
public void setTravellingAllowance(double travellingAllowance) {
this.travellingAllowance = travellingAllowance;
}
public double getTaxPercentage() {
return taxPercentage;
}
public void setTaxPercentage(double taxPercentage) {
this.taxPercentage = taxPercentage;
}
public double getSalaryAmount() {
return salaryAmount;
}
public void setSalaryAmount(double salaryAmount) {
this.salaryAmount = salaryAmount;
}
}
ProcessingFactory.java
package com.goodhope.fas.drools.factory;
/**
* Created with IntelliJ IDEA.
* User: Gayan Dissanayake
* Date: 02.11.15
*/
public interface ProcessingFactory
T createProcessingObject(V inputObject);
}
ProcessingFactoryImpl.java
package com.goodhope.fas.drools.factory.drools;
import com.goodhope.fas.drools.factory.ProcessingFactory;
import org.drools.runtime.StatefulKnowledgeSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
/**
* Created with IntelliJ IDEA.
* User: Gayan Dissanayake
* Date: 02.11.15
*/
@Component("ProcessingFactoryImpl")
public class ProcessingFactoryImpl implements ProcessingFactory
@Autowired
private ApplicationContext applicationContext;
@Override
public StatefulKnowledgeSession createProcessingObject(String beanId) {
return (StatefulKnowledgeSession)applicationContext.getBean(beanId);
}
}
SalaryService.java
package com.goodhope.fas.drools.service;
/**
* Created with IntelliJ IDEA.
* User: Gayan Dissanayake
* Date: 02.11.15
*/
public interface SalaryService
void calculateSalary(T input);
}
SalaryServiceImpl.java
package com.goodhope.fas.drools.service.drools;
import com.goodhope.fas.drools.factory.ProcessingFactory;
import com.goodhope.fas.drools.model.Employee;
import com.goodhope.fas.drools.service.SalaryService;
import org.drools.runtime.StatefulKnowledgeSession;
import org.drools.runtime.StatelessKnowledgeSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* Created with IntelliJ IDEA.
* User: Gayan Dissanayake
* Date: 02.11.15
*/
@Component("SalaryServiceImpl")
public class SalaryServiceImpl implements SalaryService
- > {
private static final Logger LOGGER = LoggerFactory.getLogger(SalaryServiceImpl.class);
private static final String SALARY_K_SESSION="salaryKSession";
@Autowired
@Qualifier("ProcessingFactoryImpl")
ProcessingFactory
@Override
public void calculateSalary(List
LOGGER.debug("Running employee Salary logic");
Logger logger = LoggerFactory.getLogger(SalaryServiceImpl.class);
StatefulKnowledgeSession statefulKnowledgeSession = processingFactory.createProcessingObject(SALARY_K_SESSION);
statefulKnowledgeSession.setGlobal("logger", logger);
LOGGER.debug("Running rules for employees...");
if(input!=null && !input.isEmpty()){
for(Employee employee :input){
statefulKnowledgeSession.insert(employee);
}
}
statefulKnowledgeSession.fireAllRules();
LOGGER.debug("...finished running employees.");
}
}
calling class
setter injection by spring
@Autowired
@Qualifier("SalaryServiceImpl")
SalaryService processingService;
salary calculation method
public void calculateSalary() throws SalaryCalculationException {
List
for(int i=0;i<100 br="" i=""> Employee emp1 = new Employee(EmployeeType.SKU_H);
emp1.setEmployeeName("Emp 1 Name "+i);
Employee emp2 = new Employee(EmployeeType.SKU_B);
emp2.setEmployeeName("Emp 2 Name "+i);
Employee emp3 = new Employee(EmployeeType.BHL);
emp3.setEmployeeName("Emp 3 Name "+i);
list.add(emp1);
list.add(emp2);
list.add(emp3);
System.out.println("Adding "+i);
processingService.calculateSalary(list);
}
for(Employee employee:list){
System.out.println(employee.getEmployeeName()+"-"+employee.getSalaryAmount());
}
} 100>
Wednesday, March 6, 2013
Detect Home folder using java
import javax.swing.filechooser.*;
public class DriveTypeInfo {
public static void main(String[] args)
{
FileSystemView fsv = FileSystemView.getFileSystemView();
System.out.println("Home directory: " + fsv.getHomeDirectory());
File[] f = File.listRoots();
for (File aF : f) {
System.out.println("Drive: " + aF);
System.out.println("Display name: " + fsv.getSystemDisplayName(aF));
}
}
}
Sunday, October 30, 2011
Calling EJB mehod from java class
public class TestEJBClient {
public static void main(String[] args) throws NamingException, CreateException, RemoteException {
Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");//put correct server host and port
p.put(Context.PROVIDER_URL, "jnp://localhost:1099");
Context jndiContext = new javax.naming.InitialContext(p);
SessionHome home = (SessionHome) jndiContext.lookup("example1/PublicSession");
Session bean = home.create();
//calling EJB bean method
bean.echo("Hello");
}
}
Monday, March 28, 2011
putting object to MQ and read objects from EJB Messaging Bean
========================
QueueConnection queueConnection = null;
QueueSession jmsSession = null;
try {
Hashtable hashtable=new Hashtable();
hashtable.put("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
hashtable.put("java.naming.provider.url","jnp://localhost:1099");
QueueConnection queueConnection =MessagingServiceMDBUtil.getQueueConnection(hashtable);
jmsSession = queueConnection.createQueueSession(false,QueueSession.AUTO_ACKNOWLEDGE);
QueueSender sender = jmsSession.createSender(MessagingServiceMDBUtil.getQueue(LookupUtil.lookupMessagingConfig().getJndiProperties()));
// create the message
ObjectMessage message = jmsSession.createObjectMessage();
ArrayList msgList = new ArrayList();
//Object
Student studentObj=new Student();
studentObj.setFristName("Gayan");
studentObj.setLastName("Dissanayake");
msgList.add(studentObj);
message.setObject(msgList);
//send the message
sender.send(message);
sender.close();
log.debug("Message is written to jms queue");
} catch (JMSException e) {
log.error("Sending message via jms failed", e);
} catch (NamingException e) {
log.error("Sending message via jms failed", e);
} catch(ModuleException moduleException) {
log.error("Sending message via jms failed", moduleException);
} finally {
if (queueConnection != null){
try {
queueConnection.close();
log.debug("Closed jms queue connection");
} catch (JMSException e) {
log.error("Closing jms queue connection failed", e);
}
}
if (jmsSession != null){
try {
jmsSession.close();
if (log.isDebugEnabled()) log.debug("Closed jms session");
} catch (JMSException e) {
if (log.isDebugEnabled()) log.error("Closing jms session failed", e);
}
}
}
EJB Messaging Bean
===================
public class MessagingServiceMDB implements MessageDrivenBean, MessageListener {
private static final long serialVersionUID = -6583759698965963589L;
private final Log logger = LogFactory.getLog(getClass());
public void onMessage(Message message){
try {
logger.debug("Entered MessagingServiceMDB.onMessage()");
ObjectMessage objMessage = (ObjectMessage)message;
ArrayList msgProfiles = (ArrayList)objMessage.getObject();
//do business logic here using MQ object
logger.debug("Entered MessagingServiceMDB.onMessage()");
} catch (JMSException jmsException) {
logger.error("Exception in MessagingServiceMDB:onMessage() " + jmsException.getMessage(), jmsException);
} catch (Exception exception) {
logger.error("Exception in MessagingServiceMDB:onMessage() " + exception.getMessage(), exception);
}
}
public void setMessageDrivenContext(MessageDrivenContext cxt) throws EJBException {}
public void ejbRemove() throws EJBException {}
public void ejbCreate() throws EJBException {}
}
add following to ejb-jar.xml
============================
<message-driven >
<description><![CDATA[MessagingServiceMDB Message Driven Bean]]></description>
<display-name>MessagingServiceMDB</display-name>
<ejb-name>MessagingServiceMDB</ejb-name>
<ejb-class>com.isa.holidays.messaging.service.MessagingServiceMDB</ejb-class>
<transaction-type>Container</transaction-type>
<acknowledge-mode>Auto-acknowledge</acknowledge-mode>
<message-driven-destination>
<destination-type>javax.jms.Queue</destination-type>
</message-driven-destination>
<resource-ref >
<res-ref-name>jms/QCF</res-ref-name>
<res-type>javax.jms.QueueConnectionFactory</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</message-driven>