Skip to content

Commit b21a370

Browse files
Raksha RaoRaksha Rao
authored andcommitted
Merge branch 'master' of https://github.com/raksha-rao/tutorials
2 parents dafe2f5 + 9482433 commit b21a370

17 files changed

Lines changed: 286 additions & 81 deletions

File tree

‎core-java-8/src/main/java/com/baeldung/spliteratorAPI/RelatedAuthorSpliterator.java‎

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,48 @@
22

33
import java.util.List;
44
import java.util.Spliterator;
5+
import java.util.concurrent.atomic.AtomicInteger;
56
import java.util.function.Consumer;
67

78
public class RelatedAuthorSpliterator implements Spliterator<Author> {
8-
private final List<Author> list;
9-
private int current = 0;
10-
11-
public RelatedAuthorSpliterator(List<Author> list) {
12-
this.list = list;
13-
}
14-
15-
@Override
16-
public boolean tryAdvance(Consumer<? super Author> action) {
17-
action.accept(list.get(current++));
18-
return current < list.size();
19-
}
20-
21-
@Override
22-
public Spliterator<Author> trySplit() {
23-
int currentSize = list.size() - current;
24-
if (currentSize < 10) {
25-
return null;
26-
}
27-
for (int splitPos = currentSize / 2 + current; splitPos < list.size(); splitPos++) {
28-
if (list.get(splitPos)
29-
.getRelatedArticleId() == 0) {
30-
Spliterator<Author> spliterator = new RelatedAuthorSpliterator(list.subList(current, splitPos));
31-
current = splitPos;
32-
return spliterator;
33-
}
34-
}
35-
return null;
36-
}
37-
38-
@Override
39-
public long estimateSize() {
40-
return list.size() - current;
41-
}
42-
43-
@Override
44-
public int characteristics() {
45-
return SIZED + CONCURRENT;
46-
}
9+
private final List<Author> list;
10+
AtomicInteger current = new AtomicInteger();
11+
12+
public RelatedAuthorSpliterator(List<Author> list) {
13+
this.list = list;
14+
}
15+
16+
@Override
17+
public boolean tryAdvance(Consumer<? super Author> action) {
18+
19+
action.accept(list.get(current.getAndIncrement()));
20+
return current.get() < list.size();
21+
}
22+
23+
@Override
24+
public Spliterator<Author> trySplit() {
25+
int currentSize = list.size() - current.get();
26+
if (currentSize < 10) {
27+
return null;
28+
}
29+
for (int splitPos = currentSize / 2 + current.intValue(); splitPos < list.size(); splitPos++) {
30+
if (list.get(splitPos).getRelatedArticleId() == 0) {
31+
Spliterator<Author> spliterator = new RelatedAuthorSpliterator(list.subList(current.get(), splitPos));
32+
current.set(splitPos);
33+
return spliterator;
34+
}
35+
}
36+
return null;
37+
}
38+
39+
@Override
40+
public long estimateSize() {
41+
return list.size() - current.get();
42+
}
43+
44+
@Override
45+
public int characteristics() {
46+
return CONCURRENT;
47+
}
48+
4749
}

‎spring-5-reactive/src/main/java/com/baeldung/reactive/websocket/ReactiveJavaClientWebSocket.java‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,25 @@
22

33
import java.net.URI;
44
import java.time.Duration;
5-
import org.springframework.boot.autoconfigure.SpringBootApplication;
65
import org.springframework.web.reactive.socket.WebSocketMessage;
76
import org.springframework.web.reactive.socket.client.ReactorNettyWebSocketClient;
87
import org.springframework.web.reactive.socket.client.WebSocketClient;
98

109
import reactor.core.publisher.Mono;
1110

12-
@SpringBootApplication
1311
public class ReactiveJavaClientWebSocket {
14-
public static void main(String[] args) throws InterruptedException {
12+
public static void main(String[] args) throws InterruptedException {
13+
1514
WebSocketClient client = new ReactorNettyWebSocketClient();
16-
client.execute(URI.create("ws://localhost:8080/event-emitter"), session -> session.send(Mono.just(session.textMessage("event-me-from-spring-reactive-client")))
15+
client.execute(
16+
URI.create("ws://localhost:8080/event-emitter"),
17+
session -> session.send(
18+
Mono.just(session.textMessage("event-spring-reactive-client-websocket")))
1719
.thenMany(session.receive()
18-
.map(WebSocketMessage::getPayloadAsText)
19-
.log())
20+
.map(WebSocketMessage::getPayloadAsText)
21+
.log())
2022
.then())
2123
.block(Duration.ofSeconds(10L));
2224
}
25+
2326
}

‎spring-5-reactive/src/main/java/com/baeldung/reactive/websocket/ReactiveWebSocketHandler.java‎

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,45 +12,25 @@
1212
import reactor.core.publisher.Flux;
1313
import reactor.core.publisher.Mono;
1414

15-
import javax.annotation.PostConstruct;
1615
import java.time.Duration;
1716
import java.time.LocalDateTime;
1817
import java.util.UUID;
1918

2019
@Component
2120
public class ReactiveWebSocketHandler implements WebSocketHandler {
2221

23-
private Flux<Event> eventFlux;
24-
private Flux<Event> intervalFlux;
22+
private Flux<Event> eventFlux = Flux.generate(e -> {
23+
Event event = new Event(UUID.randomUUID().toString(), LocalDateTime.now().toString());
24+
e.next(event);
25+
});
2526

26-
/**
27-
* Here we prepare a Flux that will emit a message every second
28-
*/
29-
@PostConstruct
30-
private void init() throws InterruptedException {
27+
private Flux<Event> intervalFlux = Flux.interval(Duration.ofMillis(1000L)).zipWith(eventFlux, (time, event) -> event);
3128

32-
eventFlux = Flux.generate(e -> {
33-
Event event = new Event(UUID.randomUUID()
34-
.toString(),
35-
LocalDateTime.now()
36-
.toString());
37-
e.next(event);
38-
});
29+
private ObjectMapper json = new ObjectMapper();
3930

40-
intervalFlux = Flux.interval(Duration.ofMillis(1000L))
41-
.zipWith(eventFlux, (time, event) -> event);
42-
43-
}
44-
45-
/**
46-
* On each new client session, send the message flux to the client.
47-
* Spring subscribes to the flux and send every new flux event to the WebSocketSession object
48-
* @param session
49-
* @return Mono<Void>
50-
*/
5131
@Override
5232
public Mono<Void> handle(WebSocketSession webSocketSession) {
53-
ObjectMapper json = new ObjectMapper();
33+
5434
return webSocketSession.send(intervalFlux.map(event -> {
5535
try {
5636
String jsonEvent = json.writeValueAsString(event);
@@ -60,12 +40,9 @@ public Mono<Void> handle(WebSocketSession webSocketSession) {
6040
e.printStackTrace();
6141
return "";
6242
}
63-
})
64-
.map(webSocketSession::textMessage))
43+
}).map(webSocketSession::textMessage))
6544

66-
.and(webSocketSession.receive()
67-
.map(WebSocketMessage::getPayloadAsText)
68-
.log());
45+
.and(webSocketSession.receive().map(WebSocketMessage::getPayloadAsText).log());
6946
}
7047

7148
}

‎spring-cloud/README.md‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,11 @@
1212

1313
Client-side service discovery allows services to find and communicate with each other without hardcoding hostname and port. The only ‘fixed point’ in such an architecture consists of a service registry with which each service has to register.
1414

15+
### Relevant Articles:
1516
- [Intro to Spring Cloud Netflix - Hystrix](http://www.baeldung.com/spring-cloud-netflix-hystrix)
1617
- [Dockerizing a Spring Boot Application](http://www.baeldung.com/dockerizing-spring-boot-application)
1718
- [Introduction to Spring Cloud Rest Client with Netflix Ribbon](http://www.baeldung.com/spring-cloud-rest-client-with-netflix-ribbon)
1819
- [A Quick Guide to Spring Cloud Consul](http://www.baeldung.com/spring-cloud-consul)
19-
20-
### Relevant Articles:
21-
- [Introduction to Spring Cloud Rest Client with Netflix Ribbon](http://www.baeldung.com/spring-cloud-rest-client-with-netflix-ribbon)
2220
- [An Introduction to Spring Cloud Zookeeper](http://www.baeldung.com/spring-cloud-zookeeper)
21+
- [Using a Spring Cloud App Starter](http://www.baeldung.com/using-a-spring-cloud-app-starter)
2322
- [Spring Cloud Connectors and Heroku](http://www.baeldung.com/spring-cloud-heroku)
24-
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/usr/bin/env bash
2+
3+
# For Ubuntu 14.04
4+
# Inspired from: https://github.com/curran/setupHadoop/blob/master/setupHadoop.sh
5+
# Use from the user directory
6+
7+
# Install Java
8+
sudo apt-get update
9+
sudo add-apt-repository -y ppa:webupd8team/java
10+
sudo apt-get install -y oracle-java8-installer
11+
12+
# Install Hadoop
13+
curl -O http://mirror.cogentco.com/pub/apache/hadoop/common/hadoop-2.8.2/hadoop-2.8.2.tar.gz
14+
tar xfz hadoop-2.8.2.tar.gz
15+
sudo mv hadoop-2.8.2 /usr/local/hadoop
16+
rm hadoop-2.8.2.tar.gz
17+
18+
# Environmental Variables
19+
echo export JAVA_HOME=/usr/lib/jvm/java-8-oracle >> ~/.bashrc
20+
echo export HADOOP_PREFIX=/usr/local/hadoop >> ~/.bashrc
21+
echo export PATH=\$PATH:/usr/local/hadoop/bin >> ~/.bashrc
22+
echo export PATH=\$PATH:/usr/local/hadoop/sbin >> ~/.bashrc
23+
source ~/.bashrc
24+
25+
# Copy configuration files
26+
cp master/* /usr/local/hadoop/etc/hadoop/
27+
28+
# Format HDFS
29+
hdfs namenode -format
30+
31+
# SSH keys for Hadoop to use.
32+
ssh-keygen -t rsa -P 'password' -f ~/.ssh/id_rsa.pub
33+
sudo mv ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys
34+
35+
# SSH
36+
ssh localhost
37+
# authenticate with osboxes.org
38+
39+
# Start NameNode daemon and DataNode daemon
40+
start-dfs.sh
41+
# stop-dfs.sh
42+
43+
# Install Maven
44+
sudo apt-get install maven
45+
46+
# Access Hadoop - http://localhost:50070
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea
2+
*/target/*
3+
*.iml
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?xml version="1.0"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
4+
xmlns="http://maven.apache.org/POM/4.0.0">
5+
<modelVersion>4.0.0</modelVersion>
6+
<groupId>com.baeldung.twitterhdfs</groupId>
7+
<artifactId>twitterhdfs</artifactId>
8+
<packaging>jar</packaging>
9+
<version>1.0.0</version>
10+
11+
<name>twitterhdfs</name>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
16+
<java.version>1.8</java.version>
17+
</properties>
18+
19+
<parent>
20+
<groupId>org.springframework.boot</groupId>
21+
<artifactId>spring-boot-starter-parent</artifactId>
22+
<version>1.5.8.RELEASE</version>
23+
</parent>
24+
25+
<dependencies>
26+
<!-- Spring Stream Starter Apps -->
27+
<dependency>
28+
<groupId>org.springframework.cloud.stream.app</groupId>
29+
<artifactId>spring-cloud-starter-stream-source-twitterstream</artifactId>
30+
<version>1.3.1.RELEASE</version>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.springframework.cloud.stream.app</groupId>
34+
<artifactId>spring-cloud-starter-stream-sink-hdfs</artifactId>
35+
<version>1.3.1.RELEASE</version>
36+
</dependency>
37+
38+
<!-- JSTL/JSP -->
39+
<dependency>
40+
<groupId>javax.servlet</groupId>
41+
<artifactId>jstl</artifactId>
42+
</dependency>
43+
44+
</dependencies>
45+
46+
<build>
47+
<plugins>
48+
<plugin>
49+
<groupId>org.springframework.boot</groupId>
50+
<artifactId>spring-boot-maven-plugin</artifactId>
51+
</plugin>
52+
</plugins>
53+
<finalName>twitterhdfs</finalName>
54+
</build>
55+
</project>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.twitterhdfs.aggregate;
2+
3+
import com.baeldung.twitterhdfs.processor.ProcessorApp;
4+
import com.baeldung.twitterhdfs.source.SourceApp;
5+
import com.baeldung.twitterhdfs.sink.SinkApp;
6+
import org.springframework.boot.autoconfigure.SpringBootApplication;
7+
import org.springframework.cloud.stream.aggregate.AggregateApplicationBuilder;
8+
9+
@SpringBootApplication
10+
public class AggregateApp {
11+
public static void main(String[] args) {
12+
new AggregateApplicationBuilder()
13+
.from(SourceApp.class).args("--fixedDelay=5000")
14+
.via(ProcessorApp.class)
15+
.to(SinkApp.class).args("--debug=true")
16+
.run(args);
17+
}
18+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.twitterhdfs.processor;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.cloud.stream.annotation.EnableBinding;
7+
import org.springframework.cloud.stream.messaging.Processor;
8+
import org.springframework.integration.annotation.Transformer;
9+
10+
@SpringBootApplication
11+
@EnableBinding(Processor.class)
12+
public class ProcessorApp {
13+
Logger log = LoggerFactory.getLogger(ProcessorApp.class);
14+
15+
@Transformer(inputChannel = Processor.INPUT, outputChannel = Processor.OUTPUT)
16+
public String processMessage(String payload) {
17+
log.info("Payload received!");
18+
return payload;
19+
}
20+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.twitterhdfs.sink;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.cloud.stream.annotation.EnableBinding;
7+
import org.springframework.cloud.stream.app.hdfs.sink.HdfsSinkConfiguration;
8+
import org.springframework.cloud.stream.messaging.Sink;
9+
import org.springframework.context.annotation.Import;
10+
import org.springframework.integration.annotation.ServiceActivator;
11+
12+
@SpringBootApplication
13+
@EnableBinding(Sink.class)
14+
@Import(HdfsSinkConfiguration.class)
15+
public class SinkApp {
16+
Logger log = LoggerFactory.getLogger(SinkApp.class);
17+
18+
@ServiceActivator(inputChannel= Sink.INPUT)
19+
public void loggerSink(Object payload) {
20+
log.info("Received: " + payload);
21+
}
22+
}

0 commit comments

Comments
 (0)