Java · Java8 · sample java server

Asynchronous Server In Java

The Asynchronous Server is  a sample server program which can handle 200 concurrent connections at a time.  The fixed thread pool which is used in this program can be increased to handle more request. However it may throw out of memory as more number of thread going to use more memory.

/**
 * This server socket program has been created to test the programe with -client and -server JVM parameters.
 */
package sockets;

import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author prabhu kvn
 *
 */
public class ServerSocketDemo {
	volatile int counter =0;
	/**
	 * 
	 */
	public ServerSocketDemo() {
		// TODO Auto-generated constructor stub
	}
	
	public static void main(String[] args) {
		ServerSocketDemo demo = new ServerSocketDemo();
		demo.startServer();
	}

	private void startServer() {
		// TODO Auto-generated method stub
		try {
			ExecutorService exeutorService = Executors.newFixedThreadPool(200);
			ServerSocket serverSocket = new ServerSocket(8181,10000);
			
			
			while(true) {
			Socket socket = serverSocket.accept();
			exeutorService.submit(()->{
				try {
					counter = process(socket);
				} catch (IOException | InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			});
			
			}
			//socket.close();
			
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	private int process(Socket socket) throws IOException, SocketException, InterruptedException {
		System.out.println("Server Socket:"+socket);
		OutputStream out = socket.getOutputStream();
		out.write(("Request received"+counter).getBytes());
		counter++;
		socket.setKeepAlive(true);
		Thread.sleep(1000);
		socket.close();
		return counter;
	}

}

Uncategorized

Solr Search Engine for Desktop

                     It takes good amount of time to find a file in Windows machines. Here is simple system where we can index all the desktop files in solr search engine, a famous open source search engine and can be searched in seconds.

Solr is an open source search engine from apache solr https://lucene.apache.org/solr/.

Step 1: Download the solr and run it in cloud mode. Here is the guide to get start on solr. https://lucene.apache.org/solr/guide/7_7/solr-tutorial.html#solr-tutorial

once you download and extract the solr into a file, just start the solr with following command

>./bin/solr start -c -p 8983 -s ../example/cloud/node1/solr

This will start the first node.  This may take approximately 30 sec to start it. And it will also start zookeeper ensemble to manage the nodes. Once first node starts, here is the command to start second node.

>./bin/solr start -c -p 7574 -s ../example/cloud/node2/solr -z localhost:9983

this will start the solr engine in two node cluster and can be accessed at http://localhost:8983/solr

Step 2: Create a collection for indexing files

>solr create -c pdf-files -s 2 -rf 2

Step 3: Write a program to extract and index the files names in solr search Engines.

package com.kvn.web.solrClients;

import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.function.Consumer;

import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;

public class CustomCloudSolrClient {

	/**
	 * Solr URL, which is running in local machine.
	 */
	String baseUrl = "http://localhost:8983/solr/pdf-files";

	/**
	 * Folders to index. you may not need to index all the folders.
	 */
	String baseFoldersToIndex[] = { "C:/docs", "C:/Java Majestic", "C:/JavaMajestic", "C:/lastminute.comdocs",
			"C:/Users/prabhukvn/Documents", "c:/Users/prabhukvn/Downloads" };
	/**
	 * What kind of files you want to index.
	 */
	String fileType[] = { ".pdf", ".doc", ".docx" };

	/**
	 * A Blocking queue to control the threads.
	 */
	BlockingQueue<File> fileNameQueue = new LinkedBlockingDeque<File>(20);

	/**
	 *  It starts here.
	 * @param args
	 */
	public static void main(String[] args) {
		CustomCloudSolrClient client = new CustomCloudSolrClient();
		client.start();
	}

	/**
	 * Start the indexing process.
	 */
	private void start() {
		try {

			HttpSolrClient solrClient = connect();
			Arrays.stream(baseFoldersToIndex).forEach(applyIndex(solrClient));
			solrClient.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private Consumer<? super String> applyIndex(HttpSolrClient solrClient) {
		return baseFolderToIndex -> {
			try {
				fileNameQueue.put(new File(baseFolderToIndex));
				pushDocs(solrClient);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		};
	}

	private void pushDocs(HttpSolrClient solrClient) {
		try {
			File folderPath = fileNameQueue.take();
			File fileList[] = folderPath.listFiles();
			List<FileDoc> fileDocs = new ArrayList<FileDoc>();
			Arrays.stream(fileList).forEach(pushDocsToSolr(solrClient, fileDocs));

			if (fileDocs.size() > 0) {
				System.out.println("Indexing from" + folderPath.getAbsolutePath() + " And Size:" + fileDocs.size());
				solrClient.addBeans(fileDocs);
				solrClient.commit();
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SolrServerException e) {
			e.printStackTrace();
		} catch (InterruptedException e1) {
			e1.printStackTrace();
		}

	}

	private Consumer<? super File> pushDocsToSolr(HttpSolrClient solrClient, List<FileDoc> fileDocs) {
		return f -> {
			if (null != f) {
				Arrays.stream(fileType).forEach(element -> {

					if (f.getName().contains(element)) {
						fileDocs.add(new FileDoc(f.getName(), f.getAbsolutePath(), new Timestamp(f.lastModified())));
					}

				});
				if (f.isDirectory() && !f.getName().contains(".")) {
					try {
						fileNameQueue.put(f);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					pushDocs(solrClient);
				}
			}
		};
	}

	/**
	 * Connect to solr using solr client.
	 * 
	 * @return
	 */
	private HttpSolrClient connect() {
		return new HttpSolrClient.Builder(baseUrl).withConnectionTimeout(10000).withSocketTimeout(60000).build();

	}

}



And Maven Dependency
		<dependency>
			<groupId>org.apache.solr</groupId>
			<artifactId>solr-solrj</artifactId>
			<version>7.7.0</version>
		</dependency>

And use http://localhost:8983/solr/#/pdf-files/query to search the data.

Or http://localhost:8983/solr to open the dashboard

Uncategorized

Java Server to Handle More Concurrent Connections

The following program can handle almost 65000 (65K) request with a single instance.

/**
 * This server socket program has been created to test the programe with -client and -server JVM parameters.
 */
package sockets;

import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author prabhu kvn
 *
 */
public class ServerSocketDemoWithBlockingQueue {

	Logger logger = LoggerFactory.getLogger(this.getClass().getName());

	/**
	 * A blocking queue to handle connections. this can hold up to one lakh sockets
	 */
	/**
	 * Default values
	 */
	private int queueLength = 10000;
	int serverThreadCount = 1;
	BlockingQueue<Socket> queue = new LinkedBlockingQueue<>(queueLength);

	/**
	 * Default Constructor
	 */
	public ServerSocketDemoWithBlockingQueue() {

	}

	/**
	 * Default Constructor
	 */
	public ServerSocketDemoWithBlockingQueue(int serverThreadCount, int queueLength) {

		this.serverThreadCount = serverThreadCount;
		this.queueLength = queueLength;
		this.queue = new LinkedBlockingQueue<>(queueLength);

	}

	/**
	 * Entry point for server start up
	 * 
	 * @param args
	 */
	public static void main(String[] args) {

		ServerSocketDemoWithBlockingQueue demo = new ServerSocketDemoWithBlockingQueue();
		demo.startServer();
	}

	/**
	 * Starting the server to handle asynch connections
	 */
	private void startServer() {

		logger.info("Starting the server with {} threads and queue size {}", serverThreadCount, queueLength);
		ExecutorService service = Executors.newFixedThreadPool(serverThreadCount);
		for (int i = 0; i < serverThreadCount; i++) {
			service.submit(new ServerProcessing(queue));
		}
		ServerSocket serverSocket = null;
		try {

			// create the server socket
			serverSocket = new ServerSocket(8181, 100000);

			while (true) {
				Socket socket = serverSocket.accept();
				try {
					queue.put(socket);
				} catch (InterruptedException e) {
					logger.error("Problem in accepting new connections : {}", e);
					e.printStackTrace();
				}

			}

		} catch (IOException e) {
			logger.error("Problem in creating server socket {}", e);
			e.printStackTrace();
		} finally {
			if (null != serverSocket) {
				try {
					serverSocket.close();
					logger.info("Server Socket closed Successfully.");
				} catch (IOException e) {
					logger.error("Problem in closing server socket.");
					e.printStackTrace();
				}
			}
		}

	}

}

/**
 * Processing class
 * 
 * @author prabhukvn
 *
 */
class ServerProcessing extends Thread {
	BlockingQueue<Socket> queue;
	volatile int counter = 0;

	public ServerProcessing(BlockingQueue<Socket> queue) {
		this.queue = queue;
	}

	@Override
	public void run() {
		while (true) {
			try {

				process(queue.take());
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SocketException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	/**
	 * Execute the actual work.F
	 * @param socket
	 * @return
	 * @throws IOException
	 * @throws SocketException
	 * @throws InterruptedException
	 */
	private int process(Socket socket) throws IOException, SocketException, InterruptedException {
		System.out.println("Server Socket:" + socket);
		OutputStream out = socket.getOutputStream();
		out.write(("Request received" + counter).getBytes());
		counter++;
		socket.setKeepAlive(true);
		// Thread.sleep(1000);
		// allow client to close the connection
		socket.close();
		return counter;
	}
}

Uncategorized

Selection Sort Java Program

Selection sort is a kind of sorting mechanism where every element is compared with the every other element to sort the elements.
The following programs performs a selection sort on integers and also also calculates number of iteration required for sorting entire array. And also it simulates how the array is actually sorted on each iteration.

 


package sorting;

import java.util.Arrays;

public class SelectionSort {

public static void main(String[] rawInput) {

int numberOfIterations = 0;
int length = rawInput.length;
int input[] = new int[length];
for (int k = 0; k < length; k++) {
input[k] = Integer.parseInt(rawInput[k]);
}

System.out.println("Raw input" + Arrays.toString(input));

if (length == 0) {
System.out.println("Invalid input ");
} else {
for (int i = 0; i < length; i++) {
numberOfIterations++;
// iterate the rest of the array
for (int j = i; j input[j]) {
int temp = input[i];
input[i] = input[j];
input[j] = temp;
System.out.println(Arrays.toString(input));
}

}

}
}

System.out.println("after sorting" + Arrays.toString(input));
System.out.println("Total Number of iterations:" + numberOfIterations);

}

}

If we run the above program with following input,
Raw input[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
[8, 9, 7, 6, 5, 4, 3, 2, 1, 0]
[7, 9, 8, 6, 5, 4, 3, 2, 1, 0]
[6, 9, 8, 7, 5, 4, 3, 2, 1, 0]
[5, 9, 8, 7, 6, 4, 3, 2, 1, 0]
[4, 9, 8, 7, 6, 5, 3, 2, 1, 0]
[3, 9,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2, 3, 4, 5, 9, 8, 7, 6]
[0, 1, 2, 3, 4, 5, 8, 9, 7, 6]
[0, 1, 2, 3, 4, 5, 7, 9, 8, 6]
[0, 1, 2, 3, 4, 5, 6, 9, 8, 7]
[0, 1, 2, 3, 4, 5, 6, 8, 9, 7]
[0, 1, 2, 3, 4, 5, 6, 7, 9, 8]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
after sorting[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Total Number of iterations:65
Uncategorized

Queue with O(1) complexity and unlimited size

The queue which works with First In First Out (FIFO) mechanism. This is the queue program in java with insertion complexity of O(1) in java.

/**
*
*/
package java8pract.datastructures;

import java.util.ArrayList;
import java.util.List;

/**
* @author prabhukvn
*
*/
public class CustomQueue<T extends Comparable> {
Node firstNode;
Node lastNode;
Node tempNode;
int stackSize = 0;

public void push(T element) {
Node node = null;
if (null == firstNode) {
node = new Node(null, element, null);
firstNode = node;
stackSize++;

} else {

Node middleNode = tempNode;
node = new Node(middleNode, element, null);
middleNode.next = node;
lastNode = node;
stackSize++;
}
tempNode = node;

}

public int getSize() {
return this.stackSize;
}

public T pop() {

T element = firstNode.element;
firstNode = firstNode.next;
firstNode.previous=null;
stackSize--;
return element;

}

/**
* Get all the elements in linked list
*
* @return
*/
public List<Node> getAll() {
List<Node> list = new ArrayList();
Node temp = firstNode;
while (temp != null) {
list.add(temp);
System.out.println(temp.element);
temp = temp.next;

}
return list;
}

}

Uncategorized

Stack with O(1) push complexity

This is a stack program where the algorithm works on Last in First Out (LIFO) concept. This is a typical stack program with insertion complexity as O(1) i.e constant.


/**
*
*/
package java8pract.datastructures;

import java.util.ArrayList;
import java.util.List;

/**
* @author prabhukvn
*
*/
public class CustomStack<T extends Comparable> {
Node firstNode;
Node lastNode;
Node tempNode;
int stackSize = 0;

public void push(T element) {
Node node = null;
if (null == firstNode) {
node = new Node(null, element, null);
firstNode = node;
stackSize++;

} else {

Node middleNode = tempNode;
node = new Node(middleNode, element, null);
middleNode.next = node;
lastNode = node;
stackSize++;
}
tempNode = node;

}

public int getSize() {
return this.stackSize;
}

public T pop() {

T element = lastNode.element;
lastNode = lastNode.previous;
lastNode.next=null;
stackSize--;
return element;

}

/**
* Get all the elements in linked list
*
* @return
*/
public List<Node> getAll() {
List<Node> list = new ArrayList();
Node temp = firstNode;
while (temp != null) {
list.add(temp);
System.out.println(temp.element);
temp = temp.next;

}
return list;
}

}

Uncategorized

Linked List with O(1) Insertion Complexity

A simple linked list program where insertion complexity is O(1) order of one i.e independent of data size.

Here is the code for the same

Here is the Node/Element/Data object of linked list

Note: This program has been written with minimum code so that users can copy and experiment with it. This is not a full fledged program, hence not recommended for production use.


public class Node {

Node previous;
T element;
Node next;

public Node(Node previous, T element, Node next) {
this.previous=previous;
this.element=element;
this.next=next;

}

@Override
public String toString() {
return "Node [element=" + element + "]";
}

}

And here is the linked list implementation,

package java8pract.linkedlist_custom;
/**
* An implementation of linked list. Entire linked list is dependent on first element in the list.
*/
import java.util.ArrayList;
import java.util.List;

public class CustomLinkedList {

Node firstNode;
Node tempNode;
Node lastNode;
/**
* Add an element to the linked list
* +/----------------------------------------
* + 1. if first node is null, create a node and assign it to first node.
* + 2. and assign the newly created node to a temp Node;
* + 3. for consecutive additions, get the temp node into a middle methods level node
* + 4. Create new node and assign previous node as middle node.
* + 5.update the last node.
* + 6. middle node.next to newly created node
* + 7. and update temp node with newly created node.
* @param element
*/

public void add(T element) {
Node node = null;
if (null == firstNode) {
node = new Node(null, element, null);
firstNode = node;

} else {
Node middleNode = tempNode;
node = new Node(middleNode, element, null);
lastNode = node;
middleNode.next = node;

}
tempNode = node;
}

/**
* Get all the elements in linked list
*
* @return
*/
public List getAll() {
List list = new ArrayList();
Node temp = firstNode;
while (temp != null) {
list.add(temp);
System.out.println(temp.element);
temp = temp.next;

}
return list;
}

/**
* Get all the elements in the linked list in reverse order.
*
* @return
*/
public List getAllReverse() {
List list = new ArrayList();
Node temp = lastNode;
while (temp != null) {
list.add(temp);
System.out.println(temp.element);
temp = temp.previous;

}
return list;

}

public Node get(T t) {

Node temp = firstNode;
while (temp != null) {

if (temp.element !=null && temp.element.compareTo(t)==0) {
break;
} else {
temp = temp.next;
}
}

return temp;

}
/**
* Get the first node (HEAD) in the linked list
* @return
*/
public Node getFirstNode(){
return firstNode;
}
/**
* Get the last node in the linked list.
* @return
*/
public Node getLastNode(){
return lastNode;
}

}