Difference between CDI and EJB singleton (#6915)
* Singleton injection examples and tests added * code and tests implemented
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.singleton;
|
||||
|
||||
public class Car {
|
||||
|
||||
private String type;
|
||||
private String model;
|
||||
private boolean serviced = false;
|
||||
|
||||
public Car(String type, String model) {
|
||||
super();
|
||||
this.type = type;
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
public boolean isServiced () {
|
||||
return serviced;
|
||||
}
|
||||
|
||||
public void setServiced(Boolean serviced) {
|
||||
this.serviced = serviced;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Car [type=" + type + ", model=" + model + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.singleton;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import javax.enterprise.context.Dependent;
|
||||
|
||||
import org.springframework.web.context.annotation.RequestScope;
|
||||
|
||||
@RequestScope
|
||||
public class CarServiceBean {
|
||||
|
||||
private UUID id = UUID.randomUUID();
|
||||
|
||||
public UUID getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "CarService [id=" + id + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.baeldung.singleton;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import javax.ejb.Singleton;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@Singleton
|
||||
public class CarServiceEjbSingleton {
|
||||
|
||||
private static Logger LOG = LoggerFactory.getLogger(CarServiceEjbSingleton.class);
|
||||
|
||||
private UUID id = UUID.randomUUID();
|
||||
|
||||
private static int serviceQueue;
|
||||
|
||||
public UUID getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "CarServiceEjbSingleton [id=" + id + "]";
|
||||
}
|
||||
|
||||
public int service(Car car) {
|
||||
serviceQueue++;
|
||||
LOG.info("Car {} is being serviced @ CarServiceEjbSingleton - serviceQueue: {}", car, serviceQueue);
|
||||
simulateService(car);
|
||||
serviceQueue--;
|
||||
LOG.info("Car service for {} is completed - serviceQueue: {}", car, serviceQueue);
|
||||
return serviceQueue;
|
||||
}
|
||||
|
||||
private void simulateService(Car car) {
|
||||
try {
|
||||
Thread.sleep(100);
|
||||
car.setServiced(true);
|
||||
} catch (InterruptedException e) {
|
||||
LOG.error("CarServiceEjbSingleton::InterruptedException: {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.baeldung.singleton;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import javax.inject.Singleton;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@Singleton
|
||||
public class CarServiceSingleton {
|
||||
|
||||
private static Logger LOG = LoggerFactory.getLogger(CarServiceSingleton.class);
|
||||
|
||||
private UUID id = UUID.randomUUID();
|
||||
|
||||
private static int serviceQueue;
|
||||
|
||||
public UUID getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "CarServiceSingleton [id=" + id + "]";
|
||||
}
|
||||
|
||||
public int service(Car car) {
|
||||
serviceQueue++;
|
||||
LOG.info("Car {} is being serviced @ CarServiceSingleton - serviceQueue: {}", car, serviceQueue);
|
||||
simulateService(car);
|
||||
serviceQueue--;
|
||||
LOG.info("Car service for {} is completed - serviceQueue: {}", car, serviceQueue);
|
||||
return serviceQueue;
|
||||
|
||||
}
|
||||
|
||||
private void simulateService(Car car) {
|
||||
try {
|
||||
Thread.sleep(100);
|
||||
car.setServiced(true);
|
||||
} catch (InterruptedException e) {
|
||||
LOG.error("CarServiceSingleton::InterruptedException: {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user