Posts

Running Java Class from Terminal

How to run Java class in Terminal How to run Java class in Terminal This is to create and run one or more java files on terminal that is depends on third-party jars. This helps to quickly test some features on real environment without running E2E flow of your application. Write java file that you want to run FileCleanupUtil.java package codergists; import org.apache.commons.io.filefilter.AgeFileFilter; import java.io.File; import java.io.IOException; import java.util.Collection; import java.util.Date; import java.util.stream.Stream; import static java.time.LocalDate.now; import static java.time.ZoneId.systemDefault; import static java.util.Date.from; import static org.apache.commons.io.FileUtils.deleteDirectory; import static org.apache.commons.io.FileUtils.listFilesAndDirs; /** * Utility class cleanup archived session files which are older than 2 weeks * * @author tvajjala */ public class FileCleanupUtil { /** * Threshold time */ ...

How to create simple documentation server

Untitled To share /expose document content as a simple webserver using python and asciidoctor. Follow below steps: Create directory asciidoc Create Dockerfile as shown below Dockerfile FROM alpine RUN apk add --no-cache python3 bash sudo RUN apk add asciidoctor COPY ["*.adoc", "/"] RUN asciidoctor *.adoc ENTRYPOINT ["python3", "-m" ,"http.server", "9000"] Copy this docker file and required asciidoctor(.adoc) files to be exposed on web server Make sure to have index.adoc as default page RUN below command to create docker image build.sh docker build . -t asciidoctor RUN below command to start the server filename.sh docker run -d -p 8084:9000 --name docserver asciidoctor:latest Open http://<hostname>:8084 Last updated 2022-09-28 11:20:01 -0500

How to Implement File Locking Feature in Java

How to implement FileLocking feature in java As we know within the java application we can avoid concurrent writes/read from different threads using synchronization/Locks as shown below. Using synchronized keyword we can make complete method thread safe. Using Lock we can make some portion of the code thread safe. Synchronization.java import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class Synchronization{ public synchronized void write(){ //write some logic to avoid concurrent writes by different threads. } public void read(){ //some portion of the code Lock lock=new ReentrantLock(true); if(lock.tryLock()){ //some portion of code lock.unlock(); } //some portion of the code } } What if we want to avoid concurrent writes on a file by different applications/processes? java.nio.channels.FileChannel will help us to achieve thi...