Use cases Messaging app between two users.
GameMain is for multithreading application refers to SendMessageWrokers.java
Only 2 user need to be defined and message app needs to finalize after a restriced amount of message count.
GameMain.java
package org.nb;
import java.util.List;
import java.util.concurrent.*;
public class GameMain{
public final static int MESSAGE_LIMIT = 10;
Player initiator = new Player(0,"Initiator",true);
Player player = new Player(1,"Receiver");
SendMessageWorker messageWorker;
public GameMain(){
Player[] players =new Player[2];
players[0] = initiator;
players[1] = player;
}
public static void main(String[] args) {
GameMain gameMain = new GameMain();
gameMain.startGame();
}
public void startGame(){
Runtime.getRuntime().addShutdownHook(new Thread(() -> System.out.println("Shutdown Hook is running, Application Terminating")));
Integer counter = 0;
BlockingQueue<Runnable> blockingQueue = new ArrayBlockingQueue<>(10);
ExecutorService executorService = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, blockingQueue);
while (true) {
counter++;
messageWorker = new SendMessageWorker(counter.toString(),"Sending a new message ",initiator,player);
executorService.submit(messageWorker);
if (counter >= MESSAGE_LIMIT) {
break;
}
}
executorService.shutdown();
try {
executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
} catch (InterruptedException e) {
}
Conversation conversation = initiator.getConversationHashMap(player.getId());
List<Message> messageList = conversation.getConversation();
for(Message message : messageList){
System.out.println("Message : " + message.getContent() + " - Date :" + message.getDate() +
"- Sender " + message.getSender().getName() + "- Receiver " + message.getReceiver().getName() );
}
System.out.println("Application Terminating ...");
}
}
Conversation.java
package org.nb;
import java.util.ArrayList;
public class Conversation {
private Player participants[];
private ArrayList<Message> conversation = new ArrayList<>();
public Conversation(Player player1 , Player player2){
participants = new Player[2];
this.participants[0] = player1;
this.participants[1] = player2;
}
void addMessage(Message message){
conversation.add(message);
}
public ArrayList<Message> getConversation() {
return conversation;
}
}
Message.java
package org.nb;
import java.util.Date;
public class Message {
private String content;
private Player sender;
private Player receiver;
private Date date;
private Boolean ack = false;
public Message(String content, Player player, Player toPlayer) {
this.content = content;
this.sender = player;
this.receiver = toPlayer;
this.date = new Date();
}
public Message(String content, Player player, Player toPlayer,Boolean ack) {
this.content = content;
this.sender = player;
this.receiver = toPlayer;
this.date = new Date();
this.ack = ack;
}
public String getContent() {
return content;
}
public Player getSender() {
return sender;
}
public Player getReceiver() {
return receiver;
}
public Date getDate() {
return date;
}
}
Player.java
package org.nb;
import java.util.HashMap;
public class Player {
private int id;
private String name;
private boolean initiator = false;
private HashMap<Integer,Conversation> conversationHashMap = new HashMap<>();
public Player(int id,String name, boolean initiator){
this.id=id;
this.name=name;
this.initiator=initiator;
}
public Player(int id, String name){
this.id=id;
this.name=name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public boolean isInitiator() {
return initiator;
}
public Conversation getConversationHashMap(int id) {
return conversationHashMap.get(id);
}
public void setConversationHashMap(Integer playerId, Conversation conversation) {
this.conversationHashMap.put(playerId, conversation);
}
}
SendWorkerMessage.java
package org.nb;
public class SendMessageWorker implements Runnable {
private String name = "SendMessageTask";
String content;
Player initiator;
Player receiver;
private String counter;
public SendMessageWorker (String counter,String content,Player initiator, Player receiver) {
this.counter = counter;
this.content = content;
this.initiator = initiator;
this.receiver = receiver;
}
@Override
public void run() {
if (initiator.isInitiator() == false) {
System.out.println("Only initiator can send message");
return;
}
Conversation conversation = initiator.getConversationHashMap(receiver.getId());
if (conversation == null) {
conversation = new Conversation(initiator, receiver);
}
Message message = new Message(content, initiator, receiver);
conversation.addMessage(message);
Message messageAck = new Message(content.concat(String.valueOf("_" + counter)), receiver, initiator, true);
conversation.addMessage(messageAck);
initiator.setConversationHashMap(receiver.getId(), conversation);
receiver.setConversationHashMap(initiator.getId(), conversation);
}
}