Merge pull request #4 from eugenp/master

Sync merge
This commit is contained in:
smokeyrobot 2020-02-28 19:36:57 -05:00 committed by GitHub
commit 98cd6c524a
31 changed files with 163 additions and 124 deletions

View File

@ -5,3 +5,4 @@ This module contains articles about Scala's core features
### Relevant Articles:
- [Introduction to Scala](https://www.baeldung.com/scala-intro)
- [Regular Expressions in Scala](https://www.baeldung.com/scala/regular-expressions)

View File

@ -1,4 +1,4 @@
package com.baeldung.scala
package com.baeldung.scala.regex
import org.junit.Test
import org.junit.Assert.assertEquals

View File

@ -11,7 +11,7 @@
<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>

View File

@ -12,7 +12,7 @@
<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>
<dependencyManagement>

View File

@ -31,3 +31,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
- [Spring Shutdown Callbacks](https://www.baeldung.com/spring-shutdown-callbacks)
- [Container Configuration in Spring Boot 2](https://www.baeldung.com/embeddedservletcontainercustomizer-configurableembeddedservletcontainer-spring-boot)
- [Validation in Spring Boot](https://www.baeldung.com/spring-boot-bean-validation)
- [The BeanDefinitionOverrideException in Spring Boot](https://www.baeldung.com/spring-boot-bean-definition-override-exception)

View File

@ -35,7 +35,7 @@
<module>spring-cloud-archaius</module>
<module>spring-cloud-functions</module>
<module>spring-cloud-vault</module>
<!-- <module>spring-cloud-security</module> --> <!-- Fixing in BAEL-10887 -->
<module>spring-cloud-security</module>
<module>spring-cloud-task</module>
<module>spring-cloud-zuul</module>
<module>spring-cloud-zuul-fallback</module>

View File

@ -6,9 +6,18 @@ import javax.ejb.Singleton;
public class CounterEJB implements CounterEJBRemote {
private int count = 1;
private String name;
public int count() {
return count++;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@ -5,4 +5,6 @@ import javax.ejb.Remote;
@Remote
public interface CounterEJBRemote {
int count();
String getName();
void setName(String name);
}

View File

@ -7,7 +7,7 @@ import javax.ejb.Stateful;
@Stateful
public class ShoppingCartEJB implements ShoppingCartEJBRemote {
private String name;
private List<String> shoppingCart;
public ShoppingCartEJB() {
@ -22,4 +22,11 @@ public class ShoppingCartEJB implements ShoppingCartEJBRemote {
return shoppingCart;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}

View File

@ -10,4 +10,8 @@ public interface ShoppingCartEJBRemote {
void addItem(String item);
List<String> getItems();
void setName(String name);
String getName();
}

View File

@ -5,8 +5,18 @@ import org.springframework.stereotype.Component;
@Component
public class CounterBean {
private int count = 1;
private String name;
public int count() {
return count++;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@ -11,6 +11,7 @@ import org.springframework.stereotype.Component;
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class ShoppingCartBean {
private String name;
private List<String> shoppingCart;
public ShoppingCartBean() {
@ -25,4 +26,11 @@ public class ShoppingCartBean {
return shoppingCart;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}

View File

@ -1,9 +1,7 @@
package com.baeldung.ejb.spring.comparison.ejb;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import javax.annotation.Resource;
import javax.ejb.EJB;
@ -51,7 +49,7 @@ public class EJBUnitTest {
public static void start() throws NamingException {
ejbContainer = EJBContainer.createEJBContainer();
}
@Before
public void initializeContext() throws NamingException {
context = ejbContainer.getContext();
@ -60,42 +58,44 @@ public class EJBUnitTest {
@Test
public void givenSingletonBean_whenCounterInvoked_thenCountIsIncremented() throws NamingException {
int count = 0;
CounterEJBRemote counterEJB = (CounterEJBRemote) context.lookup("java:global/ejb-beans/CounterEJB");
CounterEJBRemote firstCounter = (CounterEJBRemote) context.lookup("java:global/ejb-beans/CounterEJB");
firstCounter.setName("first");
for (int i = 0; i < 10; i++)
count = counterEJB.count();
assertThat(count, is(not(1)));
}
@Test
public void givenSingletonBean_whenCounterInvokedAgain_thenCountIsIncremented() throws NamingException {
CounterEJBRemote counterEJB = (CounterEJBRemote) context.lookup("java:global/ejb-beans/CounterEJB");
for (int i = 0; i < 10; i++) {
count = firstCounter.count();
}
int count = 0;
for (int i = 0; i < 10; i++)
count = counterEJB.count();
assertEquals(10, count);
assertEquals("first", firstCounter.getName());
CounterEJBRemote secondCounter = (CounterEJBRemote) context.lookup("java:global/ejb-beans/CounterEJB");
int count2 = 0;
for (int i = 0; i < 10; i++) {
count2 = secondCounter.count();
}
assertEquals(20, count2);
assertEquals("first", secondCounter.getName());
assertThat(count, is(not(1)));
}
@Test
public void givenStatefulBean_whenBathingCartWithThreeItemsAdded_thenItemsSizeIsThree() throws NamingException {
ShoppingCartEJBRemote bathingCart = (ShoppingCartEJBRemote) context.lookup("java:global/ejb-beans/ShoppingCartEJB");
bathingCart.setName("bathingCart");
bathingCart.addItem("soap");
bathingCart.addItem("shampoo");
bathingCart.addItem("oil");
assertEquals(3, bathingCart.getItems()
.size());
}
assertEquals("bathingCart", bathingCart.getName());
@Test
public void givenStatefulBean_whenFruitCartWithTwoItemsAdded_thenItemsSizeIsTwo() throws NamingException {
ShoppingCartEJBRemote fruitCart = (ShoppingCartEJBRemote) context.lookup("java:global/ejb-beans/ShoppingCartEJB");
fruitCart.addItem("apples");
@ -103,6 +103,7 @@ public class EJBUnitTest {
assertEquals(2, fruitCart.getItems()
.size());
assertNull(fruitCart.getName());
}
@Test
@ -131,10 +132,7 @@ public class EJBUnitTest {
}
@AfterClass
public static void checkTotalCountAndcloseContext() throws NamingException {
CounterEJBRemote counterEJB = (CounterEJBRemote) context.lookup("java:global/ejb-beans/CounterEJB");
assertEquals(21, counterEJB.count());
public static void closeContext() throws NamingException {
context.close();
ejbContainer.close();
}

View File

@ -1,9 +1,7 @@
package com.baeldung.ejb.spring.comparison.spring;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import javax.naming.NamingException;
@ -46,40 +44,44 @@ public class SpringUnitTest {
@Test
public void whenCounterInvoked_thenCountIsIncremented() throws NamingException {
CounterBean counterBean = context.getBean(CounterBean.class);
CounterBean firstCounter = context.getBean(CounterBean.class);
firstCounter.setName("first");
int count = 0;
for (int i = 0; i < 10; i++)
count = counterBean.count();
for (int i = 0; i < 10; i++) {
count = firstCounter.count();
}
assertThat(count, is(not(1)));
}
assertEquals(10, count);
assertEquals("first", firstCounter.getName());
@Test
public void whenCounterInvokedAgain_thenCountIsIncremented() throws NamingException {
CounterBean counterBean = context.getBean(CounterBean.class);
CounterBean secondCounter = context.getBean(CounterBean.class);
int count = 0;
for (int i = 0; i < 10; i++)
count = counterBean.count();
int count2 = 0;
for (int i = 0; i < 10; i++) {
count2 = secondCounter.count();
}
assertEquals(20, count2);
assertEquals("first", secondCounter.getName());
assertThat(count, is(not(1)));
}
@Test
public void whenBathingCartWithThreeItemsAdded_thenItemsSizeIsThree() throws NamingException {
ShoppingCartBean bathingCart = context.getBean(ShoppingCartBean.class);
bathingCart.setName("bathingCart");
bathingCart.addItem("soap");
bathingCart.addItem("shampoo");
bathingCart.addItem("oil");
assertEquals(3, bathingCart.getItems()
.size());
}
@Test
public void whenFruitCartWithTwoItemsAdded_thenItemsSizeIsTwo() throws NamingException {
assertEquals("bathingCart", bathingCart.getName());
ShoppingCartBean fruitCart = context.getBean(ShoppingCartBean.class);
fruitCart.addItem("apples");
@ -87,6 +89,7 @@ public class SpringUnitTest {
assertEquals(2, fruitCart.getItems()
.size());
assertNull(fruitCart.getName());
}
@Test
@ -98,10 +101,7 @@ public class SpringUnitTest {
}
@AfterClass
public static void checkTotalCountAndcloseContext() throws NamingException {
CounterBean counterBean = context.getBean(CounterBean.class);
int count = counterBean.count();
assertEquals(21, count);
public static void closeContext() throws NamingException {
context.close();
}

View File

@ -25,7 +25,7 @@ public class KafkaApplication {
public static void main(String[] args) throws Exception {
ConfigurableApplicationContext context = SpringApplication.run(KafkaApplication.class, args);
MessageProducer producer = context.getBean(MessageProducer.class);
MessageListener listener = context.getBean(MessageListener.class);
/*
@ -101,15 +101,17 @@ public class KafkaApplication {
private String greetingTopicName;
public void sendMessage(String message) {
ListenableFuture<SendResult<String, String>> future = kafkaTemplate.send(topicName, message);
future.addCallback(new ListenableFutureCallback<SendResult<String, String>>() {
@Override
public void onSuccess(SendResult<String, String> result) {
System.out.println("Sent message=[" + message + "] with offset=[" + result.getRecordMetadata().offset() + "]");
System.out.println("Sent message=[" + message + "] with offset=[" + result.getRecordMetadata()
.offset() + "]");
}
@Override
public void onFailure(Throwable ex) {
System.out.println("Unable to send message=[" + message + "] due to : " + ex.getMessage());
@ -158,7 +160,7 @@ public class KafkaApplication {
latch.countDown();
}
@KafkaListener(topicPartitions = @TopicPartition(topic = "${partitioned.topic.name}", partitions = { "0", "3" }))
@KafkaListener(topicPartitions = @TopicPartition(topic = "${partitioned.topic.name}", partitions = { "0", "3" }), containerFactory = "partitionsKafkaListenerContainerFactory")
public void listenToParition(@Payload String message, @Header(KafkaHeaders.RECEIVED_PARTITION_ID) int partition) {
System.out.println("Received Message: " + message + " from partition: " + partition);
this.partitionLatch.countDown();

View File

@ -29,7 +29,7 @@ public class KafkaConsumerConfig {
props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
return new DefaultKafkaConsumerFactory<>(props);
}
public ConcurrentKafkaListenerContainerFactory<String, String> kafkaListenerContainerFactory(String groupId) {
ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>();
factory.setConsumerFactory(consumerFactory(groupId));
@ -50,12 +50,12 @@ public class KafkaConsumerConfig {
public ConcurrentKafkaListenerContainerFactory<String, String> headersKafkaListenerContainerFactory() {
return kafkaListenerContainerFactory("headers");
}
@Bean
public ConcurrentKafkaListenerContainerFactory<String, String> partitionsKafkaListenerContainerFactory() {
return kafkaListenerContainerFactory("partitions");
}
@Bean
public ConcurrentKafkaListenerContainerFactory<String, String> filterKafkaListenerContainerFactory() {
ConcurrentKafkaListenerContainerFactory<String, String> factory = kafkaListenerContainerFactory("filter");

View File

@ -32,7 +32,7 @@ public class KafkaProducerConfig {
public KafkaTemplate<String, String> kafkaTemplate() {
return new KafkaTemplate<>(producerFactory());
}
@Bean
public ProducerFactory<String, Greeting> greetingProducerFactory() {
Map<String, Object> configProps = new HashMap<>();
@ -41,10 +41,10 @@ public class KafkaProducerConfig {
configProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer.class);
return new DefaultKafkaProducerFactory<>(configProps);
}
@Bean
public KafkaTemplate<String, Greeting> greetingKafkaTemplate() {
return new KafkaTemplate<>(greetingProducerFactory());
}
}

View File

@ -12,10 +12,10 @@ import org.springframework.kafka.core.KafkaAdmin;
@Configuration
public class KafkaTopicConfig {
@Value(value = "${kafka.bootstrapAddress}")
private String bootstrapAddress;
@Value(value = "${message.topic.name}")
private String topicName;
@ -27,31 +27,31 @@ public class KafkaTopicConfig {
@Value(value = "${greeting.topic.name}")
private String greetingTopicName;
@Bean
public KafkaAdmin kafkaAdmin() {
Map<String, Object> configs = new HashMap<>();
configs.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapAddress);
return new KafkaAdmin(configs);
}
@Bean
public NewTopic topic1() {
return new NewTopic(topicName, 1, (short) 1);
return new NewTopic(topicName, 1, (short) 1);
}
@Bean
public NewTopic topic2() {
return new NewTopic(partionedTopicName, 6, (short) 1);
return new NewTopic(partionedTopicName, 6, (short) 1);
}
@Bean
public NewTopic topic3() {
return new NewTopic(filteredTopicName, 1, (short) 1);
return new NewTopic(filteredTopicName, 1, (short) 1);
}
@Bean
public NewTopic topic4() {
return new NewTopic(greetingTopicName, 1, (short) 1);
return new NewTopic(greetingTopicName, 1, (short) 1);
}
}

View File

@ -1,8 +1,7 @@
package com.baeldung;
package com.baeldung.reactorbus;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import reactor.Environment;
import reactor.bus.EventBus;
@ -10,13 +9,12 @@ import reactor.bus.EventBus;
public class Config {
@Bean
Environment env() {
public Environment env() {
return Environment.initializeIfEmpty().assignErrorJournal();
}
@Bean
EventBus createEventBus(Environment env) {
public EventBus createEventBus(Environment env) {
return EventBus.create(env, Environment.THREAD_POOL);
}
}

View File

@ -1,24 +1,16 @@
package com.baeldung;
package com.baeldung.reactorbus;
import com.baeldung.reactorbus.consumer.NotificationConsumer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import com.baeldung.consumer.NotificationConsumer;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import reactor.bus.EventBus;
import static reactor.bus.selector.Selectors.$;
@Configuration
@EnableAutoConfiguration
@ComponentScan
@Import(Config.class)
public class Application implements CommandLineRunner {
@SpringBootApplication
public class NotificationApplication implements CommandLineRunner {
@Autowired
private EventBus eventBus;
@ -32,7 +24,6 @@ public class Application implements CommandLineRunner {
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
SpringApplication.run(NotificationApplication.class, args);
}
}

View File

@ -1,10 +1,10 @@
package com.baeldung.consumer;
package com.baeldung.reactorbus.consumer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baeldung.doman.NotificationData;
import com.baeldung.service.NotificationService;
import com.baeldung.reactorbus.domain.NotificationData;
import com.baeldung.reactorbus.service.NotificationService;
import reactor.bus.Event;
import reactor.fn.Consumer;

View File

@ -1,11 +1,11 @@
package com.baeldung.controller;
package com.baeldung.reactorbus.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import com.baeldung.doman.NotificationData;
import com.baeldung.reactorbus.domain.NotificationData;
import reactor.bus.Event;
import reactor.bus.EventBus;

View File

@ -1,4 +1,4 @@
package com.baeldung.doman;
package com.baeldung.reactorbus.domain;
public class NotificationData {

View File

@ -1,6 +1,6 @@
package com.baeldung.service;
package com.baeldung.reactorbus.service;
import com.baeldung.doman.NotificationData;
import com.baeldung.reactorbus.domain.NotificationData;
public interface NotificationService {

View File

@ -1,9 +1,9 @@
package com.baeldung.service.impl;
package com.baeldung.reactorbus.service.impl;
import org.springframework.stereotype.Service;
import com.baeldung.doman.NotificationData;
import com.baeldung.service.NotificationService;
import com.baeldung.reactorbus.domain.NotificationData;
import com.baeldung.reactorbus.service.NotificationService;
@Service
public class NotificationServiceimpl implements NotificationService {

View File

@ -1,14 +0,0 @@
package com.baeldung;
import org.junit.Test;
import org.springframework.web.client.RestTemplate;
public class DataLoaderLiveTest {
@Test
public void exampleTest() {
RestTemplate restTemplate = new RestTemplate();
restTemplate.getForObject("http://localhost:8080/startNotification/10", String.class);
}
}

View File

@ -1,14 +1,14 @@
package org.baeldung;
package com.baeldung;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.baeldung.Application;
import com.baeldung.reactorbus.NotificationApplication;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
@SpringBootTest(classes = NotificationApplication.class)
public class SpringContextTest {
@Test

View File

@ -0,0 +1,22 @@
package com.baeldung.reactorbus;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.client.RestTemplate;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class NotificationApplicationIntegrationTest {
@LocalServerPort
private int port;
@Test
public void givenAppStarted_whenNotificationTasksSubmitted_thenProcessed() {
RestTemplate restTemplate = new RestTemplate();
restTemplate.getForObject("http://localhost:" + port + "/startNotification/10", String.class);
}
}