Difference between CDI and EJB singleton (#6915)
* Singleton injection examples and tests added * code and tests implemented
This commit is contained in:
parent
57ec69ad3b
commit
8209ec4a1f
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,142 @@
|
|||
package com.baeldung.singleton;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import javax.ejb.EJB;
|
||||
import javax.inject.Inject;
|
||||
|
||||
import org.jboss.arquillian.container.test.api.Deployment;
|
||||
import org.jboss.arquillian.junit.Arquillian;
|
||||
import org.jboss.shrinkwrap.api.ShrinkWrap;
|
||||
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
|
||||
import org.jboss.shrinkwrap.api.spec.JavaArchive;
|
||||
import org.junit.Before;
|
||||
import org.junit.FixMethodOrder;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.MethodSorters;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@RunWith(Arquillian.class)
|
||||
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
|
||||
public class CarServiceIntegrationTest {
|
||||
|
||||
public static final Logger LOG = LoggerFactory.getLogger(CarServiceIntegrationTest.class);
|
||||
|
||||
@Deployment
|
||||
public static JavaArchive createDeployment() {
|
||||
return ShrinkWrap.create(JavaArchive.class)
|
||||
.addClasses(CarServiceBean.class, CarServiceSingleton.class, CarServiceEjbSingleton.class, Car.class)
|
||||
.addAsResource("META-INF/persistence.xml")
|
||||
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
|
||||
}
|
||||
|
||||
@Inject
|
||||
private CarServiceBean carServiceBean;
|
||||
|
||||
@Inject
|
||||
private CarServiceSingleton carServiceSingleton;
|
||||
|
||||
@EJB
|
||||
private CarServiceEjbSingleton carServiceEjbSingleton;
|
||||
|
||||
private static Map<String, UUID> idMap = new HashMap<>();
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
// populate idMap only on first run
|
||||
if (idMap.isEmpty()) {
|
||||
LOG.info("setUp::carServiceBean: {}", carServiceBean.getId());
|
||||
idMap.put("carServiceBeanId", carServiceBean.getId());
|
||||
|
||||
LOG.info("setUp::carServiceSingleton: {}", carServiceSingleton.getId());
|
||||
idMap.put("carServiceSingletonId", carServiceSingleton.getId());
|
||||
|
||||
LOG.info("setUp::carServiceEjbSingleton: {}", carServiceEjbSingleton.getId());
|
||||
idMap.put("carServiceEjbSingletonId", carServiceEjbSingleton.getId());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRun1_whenGetId_thenSingletonIdEqual() {
|
||||
int testRun = 1;
|
||||
|
||||
assertNotNull(carServiceBean);
|
||||
assertNotNull(carServiceSingleton);
|
||||
assertNotNull(carServiceEjbSingleton);
|
||||
|
||||
UUID carServiceBeanId = carServiceBean.getId();
|
||||
assertEquals(idMap.get("carServiceBeanId"), carServiceBeanId);
|
||||
LOG.info("Test run {}::carServiceBeanId: {}", testRun, carServiceBeanId);
|
||||
|
||||
UUID carServiceSingletonId = carServiceSingleton.getId();
|
||||
assertEquals(idMap.get("carServiceSingletonId"), carServiceSingletonId);
|
||||
LOG.info("Test run {}::carServiceSingletonId: {}", testRun, carServiceSingletonId);
|
||||
|
||||
UUID carServiceEjbSingletonId = carServiceEjbSingleton.getId();
|
||||
assertEquals(idMap.get("carServiceEjbSingletonId"), carServiceEjbSingletonId);
|
||||
LOG.info("Test run {}::carServiceEjbSingletonId: {}", testRun, carServiceEjbSingletonId);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRun2_whenGetId_thenSingletonIdEqual() {
|
||||
int testRun = 2;
|
||||
|
||||
assertNotNull(carServiceBean);
|
||||
assertNotNull(carServiceSingleton);
|
||||
assertNotNull(carServiceEjbSingleton);
|
||||
|
||||
UUID carServiceBeanId = carServiceBean.getId();
|
||||
assertNotEquals(idMap.get("carServiceBeanId"), carServiceBeanId);
|
||||
LOG.info("Test run {}::carServiceBeanId: {}", testRun, carServiceBeanId);
|
||||
|
||||
UUID carServiceSingletonId = carServiceSingleton.getId();
|
||||
assertEquals(idMap.get("carServiceSingletonId"), carServiceSingletonId);
|
||||
LOG.info("Test run {}::carServiceSingletonId: {}", testRun, carServiceSingletonId);
|
||||
|
||||
UUID carServiceEjbSingletonId = carServiceEjbSingleton.getId();
|
||||
assertEquals(idMap.get("carServiceEjbSingletonId"), carServiceEjbSingletonId);
|
||||
LOG.info("Test run {}::carServiceEjbSingletonId: {}", testRun, carServiceEjbSingletonId);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRun3_whenSingleton_thenNoLocking() {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
String model = Double.toString(Math.round(Math.random() * 100));
|
||||
Car car = new Car("Speedster", model);
|
||||
int serviceQueue = carServiceSingleton.service(car);
|
||||
assertTrue(serviceQueue < 10);
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRun4_whenEjb_thenLocking() {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
String model = Double.toString(Math.round(Math.random() * 100));
|
||||
Car car = new Car("Speedster", model);
|
||||
int serviceQueue = carServiceEjbSingleton.service(car);
|
||||
assertEquals(0, serviceQueue);
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue