socket単純実現jms(消費者生産者モデル)
11299 ワード
本論文では,socket通信,およびlockロックメカニズムに基づいてjmsの非同期キューを初歩的に実現した.設計は、情報キュー管理クラス、サービス側クラス、クライアントクラスの3つの部分に分かれています.
Buffer(情報キュー管理クラス)
Server(サービス・エンド・クラス)
Client(クライアントクラス)
Buffer(情報キュー管理クラス)
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class Buffer {
private static Queue
Server(サービス・エンド・クラス)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import org.apache.commons.lang3.StringUtils;
public class Server extends Thread{
private Socket socket;
public Server(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
BufferedReader reader;
try {
InputStream in = socket.getInputStream();
reader = new BufferedReader(
new InputStreamReader(
in));
handle(socket,reader.readLine());
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void handle(Socket socket, String msg) throws IOException{
PrintWriter pw = new PrintWriter(socket.getOutputStream());
if (StringUtils.isNotBlank(msg)) {
if (msg.contains("add")) {
msg = msg.substring(msg.indexOf("add")+4);
Buffer.getIntance().produce(msg);
pw.write("server:add "+ msg +" to queue successfully");
}else if(msg.contains("poll")){
String consumeMsg = (String) Buffer.getIntance().consume();
pw.write("server:remove "+ consumeMsg +" from queue successfully");
}else if(msg.contains("size")){
pw.write("server:size is "+ Buffer.getIntance().getQueueSize());
}else{
pw.write("server:no such command");
}
}else{
pw.write("server:blank message");
}
pw.flush();
socket.shutdownOutput();
pw.close();
}
@SuppressWarnings("resource")
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(6666);
while(true){
new Server(serverSocket.accept()).start();
}
}
}
Client(クライアントクラス)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class Client {
private Socket socket;
private String serverIP;
private int port;
public Client(String serverIP,int port) throws UnknownHostException, IOException{
this.serverIP = serverIP;
this.port = port;
}
public void run() throws IOException{
while (true) {
socket = new Socket(serverIP, port);
input();
}
}
@SuppressWarnings("resource")
public void input() throws IOException{
Scanner scanner = new Scanner(System.in);
String servermsg = scanner.nextLine();
PrintWriter pw = new PrintWriter(socket.getOutputStream());
pw.write(servermsg);
pw.flush();
socket.shutdownOutput();
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String msg;
while ((msg = br.readLine()) != null) {
System.out.println(msg);
}
pw.close();
br.close();
}
public static void main(String[] args) throws UnknownHostException, IOException {
Client c = new Client("127.0.0.1",6666);
c.run();
}
}