Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
vanphuoc3012 committed Mar 9, 2023
0 parents commit 12b1f61
Show file tree
Hide file tree
Showing 68 changed files with 3,211 additions and 0 deletions.
33 changes: 33 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/

### VS Code ###
.vscode/
Binary file added .mvn/wrapper/maven-wrapper.jar
Binary file not shown.
18 changes: 18 additions & 0 deletions .mvn/wrapper/maven-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.8.7/apache-maven-3.8.7-bin.zip
wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.1/maven-wrapper-3.1.1.jar
5 changes: 5 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM openjdk:11
COPY ./target/ChatApplication-0.0.1-SNAPSHOT.jar /usr/src/chatapplication/
WORKDIR /usr/src/chatapplication
EXPOSE 8080
CMD ["java", "-jar", "ChatApplication-0.0.1-SNAPSHOT.jar"]
85 changes: 85 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Week 10-3, Practice

## Section 1:

### Task 1: Create Chat Application Using Websocket:

Overview:
- Oracle Database: store user information
- Websocket: (<a href="">See here</a>)
- RabbitMQ: STOMP broker, keep track of subscriptions and broadcasts messages to subscribed users. (Alternation for default in-memory STOMP broker of Spring) (<a href="src%2Fmain%2Fjava%2Fcom%2Fexample%2Fchatapplication%2Fconfiguration%2FWebSocketConfiguration.java">See here</a>)
- Redis: in-memory database, use for store WebSocket Session, Chat Room information and messages. (<a href="src%2Fmain%2Fjava%2Fcom%2Fexample%2Fchatapplication%2Fchatroom%2Fservice%2Fimplement">See here</a>)
- Apache Zookeeper
- Apache Kafka: (<a href="src%2Fmain%2Fjava%2Fcom%2Fexample%2Fchatapplication%2Fconfiguration%2Fkafka">See here</a>)

#### Result
- Chat Application with multiple room (multiple WebSocket channel):

![login.png](images%2Flogin.png)
![img.png](images%2Fimg.png)
![img_1.png](images%2Fimg_1.png)
## Section 2:
### Task 1: Add Kafka to project
[ProducerKafkaConfiguration.java](src%2Fmain%2Fjava%2Fcom%2Fexample%2Fchatapplication%2Fconfiguration%2Fkafka%2FProducerKafkaConfiguration.java)
[ConsumerKafkaConfiguration.java](src%2Fmain%2Fjava%2Fcom%2Fexample%2Fchatapplication%2Fconfiguration%2Fkafka%2FConsumerKafkaConfiguration.java)

- Dynamic create/delete topic, also change Consumer topic at runtime:

```agsl
@Service
@Slf4j
public class KafkaService {
@Autowired
private AdminClient adminClient;
@Autowired
ConcurrentKafkaListenerContainerFactory<String, Message> listenerContainerFactory;
@Autowired
ConsumerFactory<String, Message> consumerFactory;
ConcurrentMessageListenerContainer<String, Message> listenerContainer;
public void changeTopic(String topic) throws InterruptedException {
log.info("Changing topic to: {}", topic);
if(listenerContainer != null) {
listenerContainer.stop();
Thread.sleep(2000);
listenerContainer.destroy();
Thread.sleep(2000);
}
ContainerProperties containerProperties = new ContainerProperties(topic);
containerProperties.setGroupId(RandomStringUtils.randomAlphanumeric(3));
containerProperties.setMessageListener((MessageListener<String, Message>) message -> {
System.out.println("Kafka listener, topic: " + message.topic().toString() + ", message content: " + message.value().getContent());
});
listenerContainer = new ConcurrentMessageListenerContainer<>(consumerFactory, containerProperties);
listenerContainer.start();
}
public void createTopic(String topic) {
adminClient.createTopics(List.of(TopicBuilder.name(topic).build()));
}
public void deleteTopic(String topic) {
adminClient.deleteTopics(List.of(topic));
}
}
```
### Task 2: Run project using Docker
[docker-compose.yml](docker%2Fdocker-compose.yml)

![img_5.png](images%2Fimg_5.png)


### Task 3: Dynamically create/delete Kafka topic
```agsl
public void createTopic(String topic) {
adminClient.createTopics(List.of(TopicBuilder.name(topic).build()));
}
public void deleteTopic(String topic) {
adminClient.deleteTopics(List.of(topic));
}
```
![img_6.png](images%2Fimg_6.png)
1 change: 1 addition & 0 deletions data/new 1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
user1:999999999
57 changes: 57 additions & 0 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
version: '3'

networks:
chat-app:
driver: bridge

services:
chat-app-zookeeper:
image: 'bitnami/zookeeper:latest'
ports:
- '2181:2181'
environment:
- ALLOW_ANONYMOUS_LOGIN=yes
networks:
- chat-app
chat-app-kafka:
image: 'bitnami/kafka:latest'
ports:
- '9092:9092'
environment:
- KAFKA_CFG_ZOOKEEPER_CONNECT=chat-app-zookeeper:2181
- ALLOW_ANONYMOUS_LOGIN=yes
- ALLOW_PLAINTEXT_LISTENER=yes
- KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP=CLIENT:PLAINTEXT,EXTERNAL:PLAINTEXT
- KAFKA_CFG_LISTENERS=CLIENT://:9092,EXTERNAL://localhost:9093
- KAFKA_CFG_ADVERTISED_LISTENERS=CLIENT://host.docker.internal:9092,EXTERNAL://localhost:9093
- KAFKA_CFG_INTER_BROKER_LISTENER_NAME=CLIENT
depends_on:
- chat-app-zookeeper
networks:
- chat-app
chat-app-rabbitmq-stomp:
image: 'pcloud/rabbitmq-stomp:latest'
ports:
- '5672:5672'
- '15672:15672'
- '61613:61613'
networks:
- chat-app
chatApplication:
image: chatapplication:0.0.1
ports:
- '8080:8080'
environment:
- SPRING_PROFILES_ACTIVE=docker
depends_on:
- chat-app-kafka
- chat-app-rabbitmq-stomp
networks:
- chat-app
chat-app-redis:
image: redis/redis-stack:latest
ports:
- '6381:6379'
- '8003:8001'
networks:
- chat-app
Binary file added images/img.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/img_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/img_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/img_3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/img_4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/img_5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/img_6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/login.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 12b1f61

Please sign in to comment.