Merge remote-tracking branch 'upstream/master' into BAEL-5229
This commit is contained in:
@@ -27,3 +27,4 @@ HELP.md
|
||||
|
||||
### VS Code ###
|
||||
.vscode/
|
||||
|
||||
@@ -4,15 +4,15 @@ This following table contains test URLs that can be used with the Olingo V2 demo
|
||||
|
||||
| URL | Description |
|
||||
|------------------------------------------|-------------------------------------------------|
|
||||
| `http://localhost:8180/odata/$metadata` | fetch OData metadata document |
|
||||
| `http://localhost:8180/odata/CarMakers?$top=10&$skip=10` | Get 10 entities starting at offset 10 |
|
||||
| `http://localhost:8180/odata/CarMakers?$count` | Return total count of entities in this set |
|
||||
| `http://localhost:8180/odata/CarMakers?$filter=startswith(Name,'B')` | Return entities where the *Name* property starts with 'B' |
|
||||
| `http://localhost:8180/odata/CarModels?$filter=Year eq 2008 and CarMakerDetails/Name eq 'BWM'` | Return *CarModel* entities where the *Name* property of its maker starts with 'B' |
|
||||
| `http://localhost:8180/odata/CarModels(1L)?$expand=CarMakerDetails` | Return the *CarModel* with primary key '1', along with its maker|
|
||||
| `http://localhost:8180/odata/CarModels(1L)?$select=Name,Sku` | Return the *CarModel* with primary key '1', returing only its *Name* and *Sku* properties |
|
||||
| `http://localhost:8180/odata/CarModels?$orderBy=Name asc,Sku desc` | Return *CarModel* entities, ordered by the their *Name* and *Sku* properties |
|
||||
| `http://localhost:8180/odata/CarModels?$format=json` | Return *CarModel* entities, using a JSON representation|
|
||||
| `http://localhost:8080/odata/$metadata` | fetch OData metadata document |
|
||||
| `http://localhost:8080/odata/CarMakers?$top=10&$skip=10` | Get 10 entities starting at offset 10 |
|
||||
| `http://localhost:8080/odata/CarMakers?$count` | Return total count of entities in this set |
|
||||
| `http://localhost:8080/odata/CarMakers?$filter=startswith(Name,'B')` | Return entities where the *Name* property starts with 'B' |
|
||||
| `http://localhost:8080/odata/CarModels?$filter=Year eq 2008 and CarMakerDetails/Name eq 'BWM'` | Return *CarModel* entities where the *Name* property of its maker starts with 'B' |
|
||||
| `http://localhost:8080/odata/CarModels(1L)?$expand=CarMakerDetails` | Return the *CarModel* with primary key '1', along with its maker|
|
||||
| `http://localhost:8080/odata/CarModels(1L)?$select=Name,Sku` | Return the *CarModel* with primary key '1', returing only its *Name* and *Sku* properties |
|
||||
| `http://localhost:8080/odata/CarModels?$orderBy=Name asc,Sku desc` | Return *CarModel* entities, ordered by the their *Name* and *Sku* properties |
|
||||
| `http://localhost:8080/odata/CarModels?$format=json` | Return *CarModel* entities, using a JSON representation|
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -3,16 +3,16 @@
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung.examples.olingo2</groupId>
|
||||
<artifactId>olingo2</artifactId>
|
||||
<name>olingo2</name>
|
||||
<description>Sample Olingo 2 Project</description>
|
||||
<groupId>com.baeldung.examples.olingo</groupId>
|
||||
<artifactId>apache-olingo</artifactId>
|
||||
<name>apache-olingo</name>
|
||||
<description>Sample Apache Olingo Project</description>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-boot-2</relativePath>
|
||||
<relativePath>../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
@@ -43,7 +43,7 @@
|
||||
<dependency>
|
||||
<groupId>org.apache.olingo</groupId>
|
||||
<artifactId>olingo-odata2-core</artifactId>
|
||||
<version>${olingo2.version}</version>
|
||||
<version>${olingo.version}</version>
|
||||
<!-- Avoid jax-rs version conflict by excluding Olingo's version -->
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
@@ -55,12 +55,12 @@
|
||||
<dependency>
|
||||
<groupId>org.apache.olingo</groupId>
|
||||
<artifactId>olingo-odata2-jpa-processor-core</artifactId>
|
||||
<version>${olingo2.version}</version>
|
||||
<version>${olingo.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.olingo</groupId>
|
||||
<artifactId>olingo-odata2-jpa-processor-ref</artifactId>
|
||||
<version>${olingo2.version}</version>
|
||||
<version>${olingo.version}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.eclipse.persistence</groupId>
|
||||
@@ -80,7 +80,7 @@
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<olingo2.version>2.0.11</olingo2.version>
|
||||
<olingo.version>2.0.11</olingo.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
</project>
|
||||
+4
-9
@@ -27,11 +27,6 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* ODataJPAServiceFactory implementation for our sample domain
|
||||
* @author Philippe
|
||||
*
|
||||
*/
|
||||
@Component
|
||||
public class CarsODataJPAServiceFactory extends ODataJPAServiceFactory {
|
||||
|
||||
@@ -44,7 +39,7 @@ public class CarsODataJPAServiceFactory extends ODataJPAServiceFactory {
|
||||
|
||||
/**
|
||||
* This method will be called by Olingo on every request to
|
||||
* initialize the ODataJPAContext that will be used.
|
||||
* initialize the ODataJPAContext that will be used.
|
||||
*/
|
||||
@Override
|
||||
public ODataJPAContext initializeODataJPAContext() throws ODataJPARuntimeException {
|
||||
@@ -54,14 +49,14 @@ public class CarsODataJPAServiceFactory extends ODataJPAServiceFactory {
|
||||
ODataContext octx = ctx.getODataContext();
|
||||
HttpServletRequest request = (HttpServletRequest)octx.getParameter(ODataContext.HTTP_SERVLET_REQUEST_OBJECT);
|
||||
EntityManager em = (EntityManager)request.getAttribute(JerseyConfig.EntityManagerFilter.EM_REQUEST_ATTRIBUTE);
|
||||
|
||||
|
||||
// Here we're passing the EM that was created by the EntityManagerFilter (see JerseyConfig)
|
||||
ctx.setEntityManager(new EntityManagerWrapper(em));
|
||||
ctx.setPersistenceUnitName("default");
|
||||
|
||||
|
||||
// We're managing the EM's lifecycle, so we must inform Olingo that it should not
|
||||
// try to manage transactions and/or persistence sessions
|
||||
ctx.setContainerManaged(true);
|
||||
ctx.setContainerManaged(true);
|
||||
return ctx;
|
||||
}
|
||||
|
||||
+18
-25
@@ -1,6 +1,12 @@
|
||||
package com.baeldung.examples.olingo2;
|
||||
package com.baeldung.examples.olingo2;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.apache.olingo.odata2.api.ODataServiceFactory;
|
||||
import org.apache.olingo.odata2.core.rest.ODataRootLocator;
|
||||
import org.apache.olingo.odata2.core.rest.app.ODataApplication;
|
||||
import org.glassfish.jersey.server.ResourceConfig;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
@@ -15,28 +21,15 @@ import javax.ws.rs.container.ContainerResponseFilter;
|
||||
import javax.ws.rs.core.Context;
|
||||
import javax.ws.rs.ext.Provider;
|
||||
|
||||
import org.apache.olingo.odata2.api.ODataServiceFactory;
|
||||
import org.apache.olingo.odata2.core.rest.ODataRootLocator;
|
||||
import org.apache.olingo.odata2.core.rest.app.ODataApplication;
|
||||
import org.glassfish.jersey.server.ResourceConfig;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* Jersey JAX-RS configuration
|
||||
* @author Philippe
|
||||
*
|
||||
*/
|
||||
@Component
|
||||
@ApplicationPath("/odata")
|
||||
public class JerseyConfig extends ResourceConfig {
|
||||
|
||||
|
||||
public JerseyConfig(CarsODataJPAServiceFactory serviceFactory, EntityManagerFactory emf) {
|
||||
|
||||
|
||||
|
||||
public JerseyConfig(CarsODataJPAServiceFactory serviceFactory, EntityManagerFactory emf) {
|
||||
|
||||
ODataApplication app = new ODataApplication();
|
||||
|
||||
|
||||
app
|
||||
.getClasses()
|
||||
.forEach( c -> {
|
||||
@@ -46,11 +39,11 @@ public class JerseyConfig extends ResourceConfig {
|
||||
register(c);
|
||||
}
|
||||
});
|
||||
|
||||
register(new CarsRootLocator(serviceFactory));
|
||||
|
||||
register(new CarsRootLocator(serviceFactory));
|
||||
register( new EntityManagerFilter(emf));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* This filter handles the EntityManager transaction lifecycle.
|
||||
* @author Philippe
|
||||
@@ -72,7 +65,7 @@ public class JerseyConfig extends ResourceConfig {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void filter(ContainerRequestContext ctx) throws IOException {
|
||||
public void filter(ContainerRequestContext ctx) {
|
||||
log.info("[I60] >>> filter");
|
||||
EntityManager em = this.emf.createEntityManager();
|
||||
httpRequest.setAttribute(EM_REQUEST_ATTRIBUTE, em);
|
||||
@@ -85,7 +78,7 @@ public class JerseyConfig extends ResourceConfig {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
|
||||
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) {
|
||||
|
||||
log.info("[I68] <<< filter");
|
||||
EntityManager em = (EntityManager) httpRequest.getAttribute(EM_REQUEST_ATTRIBUTE);
|
||||
+1
-1
@@ -7,7 +7,7 @@ import org.springframework.boot.web.servlet.support.SpringBootServletInitializer
|
||||
@SpringBootApplication
|
||||
public class Olingo2SampleApplication extends SpringBootServletInitializer {
|
||||
|
||||
public static void main(String[] args) {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Olingo2SampleApplication.class);
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -7,7 +7,7 @@ import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class Olingo2SampleApplicationUnitTest {
|
||||
public class Olingo2SampleApplicationIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
@@ -47,7 +47,7 @@
|
||||
|
||||
<properties>
|
||||
<apache-shiro-core-version>1.5.3</apache-shiro-core-version>
|
||||
<log4j-version>1.2.17</log4j-version>
|
||||
<log4j.version>2.17.1</log4j.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+14
-8
@@ -1,9 +1,9 @@
|
||||
package com.baeldung.differences.rdd;
|
||||
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.spark.SparkConf;
|
||||
import org.apache.spark.api.java.JavaRDD;
|
||||
@@ -12,12 +12,15 @@ import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class TransformationsUnitTest {
|
||||
|
||||
|
||||
public static final String COMMA_DELIMITER = ",(?=([^\"]*\"[^\"]*\")*[^\"]*$)";
|
||||
|
||||
private static JavaSparkContext sc;
|
||||
private static JavaRDD<String> tourists;
|
||||
|
||||
|
||||
@BeforeClass
|
||||
public static void init() {
|
||||
SparkConf conf = new SparkConf().setAppName("uppercaseCountries")
|
||||
@@ -25,8 +28,11 @@ public class TransformationsUnitTest {
|
||||
sc = new JavaSparkContext(conf);
|
||||
tourists = sc.textFile("data/Tourist.csv")
|
||||
.filter(line -> !line.startsWith("Region")); //filter header row
|
||||
|
||||
// delete previous output dir and files
|
||||
FileUtils.deleteQuietly(new File("data/output"));
|
||||
}
|
||||
|
||||
|
||||
@AfterClass
|
||||
public static void cleanup() {
|
||||
sc.close();
|
||||
@@ -39,9 +45,9 @@ public class TransformationsUnitTest {
|
||||
return columns[1].toUpperCase();
|
||||
})
|
||||
.distinct();
|
||||
|
||||
|
||||
upperCaseCountries.saveAsTextFile("data/output/uppercase.txt");
|
||||
|
||||
|
||||
upperCaseCountries.foreach(country -> {
|
||||
//replace non alphanumerical characters
|
||||
country = country.replaceAll("[^a-zA-Z]", "");
|
||||
@@ -52,9 +58,9 @@ public class TransformationsUnitTest {
|
||||
@Test
|
||||
public void whenFilterByCountry_thenShowRequestedCountryRecords() {
|
||||
JavaRDD<String> touristsInMexico = tourists.filter(line -> line.split(COMMA_DELIMITER)[1].equals("Mexico"));
|
||||
|
||||
|
||||
touristsInMexico.saveAsTextFile("data/output/touristInMexico.txt");
|
||||
|
||||
|
||||
touristsInMexico.foreach(record -> {
|
||||
assertEquals("Mexico", record.split(COMMA_DELIMITER)[1]);
|
||||
});
|
||||
|
||||
+6
-7
@@ -1,17 +1,16 @@
|
||||
package com.baeldung.collections.iterators;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* Source code https://github.com/eugenp/tutorials
|
||||
*
|
||||
* @author Santosh Thakur
|
||||
*/
|
||||
|
||||
public class Iterators {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(Iterators.class);
|
||||
|
||||
public static int failFast1() {
|
||||
ArrayList<Integer> numbers = new ArrayList<>();
|
||||
|
||||
@@ -44,7 +43,7 @@ public class Iterators {
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("using iterator's remove method = " + numbers);
|
||||
LOG.debug("using iterator's remove method = {}", numbers);
|
||||
|
||||
iterator = numbers.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
|
||||
+7
-3
@@ -3,6 +3,8 @@ package com.baeldung.collections.bitset;
|
||||
import org.junit.Test;
|
||||
import org.openjdk.jol.info.ClassLayout;
|
||||
import org.openjdk.jol.info.GraphLayout;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.BitSet;
|
||||
|
||||
@@ -10,18 +12,20 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class BitSetUnitTest {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(BitSetUnitTest.class);
|
||||
|
||||
@Test
|
||||
public void givenBoolArray_whenMemoryLayout_thenConsumeMoreThanOneBit() {
|
||||
boolean[] bits = new boolean[1024 * 1024];
|
||||
|
||||
System.out.println(ClassLayout.parseInstance(bits).toPrintable());
|
||||
LOG.debug(ClassLayout.parseInstance(bits).toPrintable());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBitSet_whenMemoryLayout_thenConsumeOneBitPerFlag() {
|
||||
BitSet bitSet = new BitSet(1024 * 1024);
|
||||
|
||||
System.out.println(GraphLayout.parseInstance(bitSet).toPrintable());
|
||||
LOG.debug(GraphLayout.parseInstance(bitSet).toPrintable());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -157,7 +161,7 @@ public class BitSetUnitTest {
|
||||
BitSet bitSet = new BitSet();
|
||||
bitSet.set(15, 25);
|
||||
|
||||
bitSet.stream().forEach(System.out::println);
|
||||
bitSet.stream().forEach(bit -> LOG.debug(String.valueOf(bit)));
|
||||
assertThat(bitSet.stream().count()).isEqualTo(10);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
+10
-5
@@ -1,9 +1,15 @@
|
||||
package com.baeldung.concurrent.prioritytaskexecution;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class Job implements Runnable {
|
||||
private String jobName;
|
||||
private JobPriority jobPriority;
|
||||
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(Job.class);
|
||||
|
||||
private final String jobName;
|
||||
private final JobPriority jobPriority;
|
||||
|
||||
public Job(String jobName, JobPriority jobPriority) {
|
||||
this.jobName = jobName;
|
||||
this.jobPriority = jobPriority != null ? jobPriority : JobPriority.MEDIUM;
|
||||
@@ -16,8 +22,7 @@ public class Job implements Runnable {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
System.out.println("Job:" + jobName +
|
||||
" Priority:" + jobPriority);
|
||||
LOGGER.debug("Job:{} Priority:{}", jobName, jobPriority);
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException ignored) {
|
||||
}
|
||||
|
||||
+6
-4
@@ -1,19 +1,21 @@
|
||||
package com.baeldung.forkjoin;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ForkJoinTask;
|
||||
import java.util.concurrent.RecursiveAction;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class CustomRecursiveAction extends RecursiveAction {
|
||||
|
||||
final Logger logger = LoggerFactory.getLogger(CustomRecursiveAction.class);
|
||||
|
||||
private String workLoad = "";
|
||||
private static final int THRESHOLD = 4;
|
||||
|
||||
private static Logger logger = Logger.getAnonymousLogger();
|
||||
|
||||
public CustomRecursiveAction(String workLoad) {
|
||||
this.workLoad = workLoad;
|
||||
}
|
||||
@@ -43,7 +45,7 @@ public class CustomRecursiveAction extends RecursiveAction {
|
||||
|
||||
private void processing(String work) {
|
||||
String result = work.toUpperCase();
|
||||
logger.info("This result - (" + result + ") - was processed by " + Thread.currentThread()
|
||||
logger.debug("This result - (" + result + ") - was processed by " + Thread.currentThread()
|
||||
.getName());
|
||||
}
|
||||
}
|
||||
|
||||
+8
-8
@@ -3,9 +3,9 @@ package com.baeldung.concurrent.prioritytaskexecution;
|
||||
import org.junit.Test;
|
||||
|
||||
public class PriorityJobSchedulerUnitTest {
|
||||
private static int POOL_SIZE = 1;
|
||||
private static int QUEUE_SIZE = 10;
|
||||
|
||||
private static final int POOL_SIZE = 1;
|
||||
private static final int QUEUE_SIZE = 10;
|
||||
|
||||
@Test
|
||||
public void whenMultiplePriorityJobsQueued_thenHighestPriorityJobIsPicked() {
|
||||
Job job1 = new Job("Job1", JobPriority.LOW);
|
||||
@@ -14,19 +14,19 @@ public class PriorityJobSchedulerUnitTest {
|
||||
Job job4 = new Job("Job4", JobPriority.MEDIUM);
|
||||
Job job5 = new Job("Job5", JobPriority.LOW);
|
||||
Job job6 = new Job("Job6", JobPriority.HIGH);
|
||||
|
||||
|
||||
PriorityJobScheduler pjs = new PriorityJobScheduler(POOL_SIZE, QUEUE_SIZE);
|
||||
|
||||
|
||||
pjs.scheduleJob(job1);
|
||||
pjs.scheduleJob(job2);
|
||||
pjs.scheduleJob(job3);
|
||||
pjs.scheduleJob(job4);
|
||||
pjs.scheduleJob(job5);
|
||||
pjs.scheduleJob(job6);
|
||||
|
||||
|
||||
// ensure no tasks is pending before closing the scheduler
|
||||
while (pjs.getQueuedTaskCount() != 0);
|
||||
|
||||
|
||||
// delay to avoid job sleep (added for demo) being interrupted
|
||||
try {
|
||||
Thread.sleep(2000);
|
||||
@@ -34,7 +34,7 @@ public class PriorityJobSchedulerUnitTest {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
|
||||
pjs.closeScheduler();
|
||||
}
|
||||
}
|
||||
|
||||
+8
-11
@@ -1,18 +1,15 @@
|
||||
package com.baeldung.forkjoin;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import com.baeldung.forkjoin.util.PoolUtil;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.ForkJoinPool;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.forkjoin.CustomRecursiveAction;
|
||||
import com.baeldung.forkjoin.CustomRecursiveTask;
|
||||
import com.baeldung.forkjoin.util.PoolUtil;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class Java8ForkJoinIntegrationTest {
|
||||
|
||||
@@ -63,11 +60,11 @@ public class Java8ForkJoinIntegrationTest {
|
||||
ForkJoinPool forkJoinPool = ForkJoinPool.commonPool();
|
||||
|
||||
forkJoinPool.execute(customRecursiveTask);
|
||||
int result = customRecursiveTask.join();
|
||||
customRecursiveTask.join();
|
||||
assertTrue(customRecursiveTask.isDone());
|
||||
|
||||
forkJoinPool.submit(customRecursiveTask);
|
||||
int resultTwo = customRecursiveTask.join();
|
||||
customRecursiveTask.join();
|
||||
assertTrue(customRecursiveTask.isDone());
|
||||
}
|
||||
|
||||
|
||||
+24
-24
@@ -1,12 +1,12 @@
|
||||
package com.baeldung.thread.join;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* Demonstrates Thread.join behavior.
|
||||
@@ -14,55 +14,55 @@ import org.junit.Test;
|
||||
*/
|
||||
public class ThreadJoinUnitTest {
|
||||
|
||||
final static Logger LOGGER = Logger.getLogger(ThreadJoinUnitTest.class.getName());
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(ThreadJoinUnitTest.class);
|
||||
|
||||
class SampleThread extends Thread {
|
||||
public int processingCount = 0;
|
||||
static class SampleThread extends Thread {
|
||||
public int processingCount;
|
||||
|
||||
SampleThread(int processingCount) {
|
||||
this.processingCount = processingCount;
|
||||
LOGGER.info("Thread " + this.getName() + " created");
|
||||
LOGGER.debug("Thread " + this.getName() + " created");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
LOGGER.info("Thread " + this.getName() + " started");
|
||||
LOGGER.debug("Thread " + this.getName() + " started");
|
||||
while (processingCount > 0) {
|
||||
try {
|
||||
Thread.sleep(1000); // Simulate some work being done by thread
|
||||
} catch (InterruptedException e) {
|
||||
LOGGER.info("Thread " + this.getName() + " interrupted.");
|
||||
LOGGER.debug("Thread " + this.getName() + " interrupted.");
|
||||
}
|
||||
processingCount--;
|
||||
LOGGER.info("Inside Thread " + this.getName() + ", processingCount = " + processingCount);
|
||||
LOGGER.debug("Inside Thread " + this.getName() + ", processingCount = " + processingCount);
|
||||
}
|
||||
LOGGER.info("Thread " + this.getName() + " exiting");
|
||||
LOGGER.debug("Thread " + this.getName() + " exiting");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNewThread_whenJoinCalled_returnsImmediately() throws InterruptedException {
|
||||
Thread t1 = new SampleThread(0);
|
||||
LOGGER.info("Invoking join.");
|
||||
LOGGER.debug("Invoking join.");
|
||||
t1.join();
|
||||
LOGGER.info("Returned from join");
|
||||
LOGGER.info("Thread state is" + t1.getState());
|
||||
LOGGER.debug("Returned from join");
|
||||
LOGGER.debug("Thread state is" + t1.getState());
|
||||
assertFalse(t1.isAlive());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStartedThread_whenJoinCalled_waitsTillCompletion()
|
||||
public void givenStartedThread_whenJoinCalled_waitsTillCompletion()
|
||||
throws InterruptedException {
|
||||
Thread t2 = new SampleThread(1);
|
||||
t2.start();
|
||||
LOGGER.info("Invoking join.");
|
||||
LOGGER.debug("Invoking join.");
|
||||
t2.join();
|
||||
LOGGER.info("Returned from join");
|
||||
LOGGER.debug("Returned from join");
|
||||
assertFalse(t2.isAlive());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStartedThread_whenTimedJoinCalled_waitsUntilTimedout()
|
||||
public void givenStartedThread_whenTimedJoinCalled_waitsUntilTimedout()
|
||||
throws InterruptedException {
|
||||
Thread t3 = new SampleThread(10);
|
||||
t3.start();
|
||||
@@ -72,18 +72,18 @@ public class ThreadJoinUnitTest {
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void givenThreadTerminated_checkForEffect_notGuaranteed()
|
||||
public void givenThreadTerminated_checkForEffect_notGuaranteed()
|
||||
throws InterruptedException {
|
||||
SampleThread t4 = new SampleThread(10);
|
||||
t4.start();
|
||||
//not guaranteed to stop even if t4 finishes.
|
||||
do {
|
||||
|
||||
} while (t4.processingCount > 0);
|
||||
|
||||
} while (t4.processingCount > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenJoinWithTerminatedThread_checkForEffect_guaranteed()
|
||||
public void givenJoinWithTerminatedThread_checkForEffect_guaranteed()
|
||||
throws InterruptedException {
|
||||
SampleThread t4 = new SampleThread(10);
|
||||
t4.start();
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT"/>
|
||||
</root>
|
||||
</configuration>
|
||||
@@ -12,7 +12,6 @@ This module contains articles about basic Java concurrency
|
||||
- [Guide to AtomicMarkableReference](https://www.baeldung.com/java-atomicmarkablereference)
|
||||
- [Why are Local Variables Thread-Safe in Java](https://www.baeldung.com/java-local-variables-thread-safe)
|
||||
- [How to Stop Execution After a Certain Time in Java](https://www.baeldung.com/java-stop-execution-after-certain-time)
|
||||
- [How to Handle InterruptedException in Java](https://www.baeldung.com/java-interrupted-exception)
|
||||
- [How to Get the Number of Threads in a Java Process](https://www.baeldung.com/java-get-number-of-threads)
|
||||
- [Set the Name of a Thread in Java](https://www.baeldung.com/java-set-thread-name)
|
||||
- [[<-- Prev]](/core-java-modules/core-java-concurrency-basic)
|
||||
- [[<-- Prev]](../core-java-concurrency-basic)[[Next -->]](../core-java-concurrency-basic-3)
|
||||
|
||||
@@ -7,12 +7,6 @@
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<logger name="org.springframework" level="WARN" />
|
||||
<logger name="org.springframework.transaction" level="WARN" />
|
||||
|
||||
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
|
||||
<logger name="org.springframework.web.servlet.mvc" level="WARN" />
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
## Core Java Concurrency Basic
|
||||
|
||||
This module contains articles about basic Java concurrency.
|
||||
|
||||
### Relevant Articles:
|
||||
|
||||
- [How to Handle InterruptedException in Java](https://www.baeldung.com/java-interrupted-exception)
|
||||
- [[<-- Prev]](../core-java-concurrency-basic-2)
|
||||
@@ -0,0 +1,27 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>core-java-concurrency-basic-3</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-concurrency-basic-3</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<build>
|
||||
<finalName>core-java-concurrency-basic-3</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user