Posts

How to Run Python Script in PM2 Server

 To run your Flask app with PM2, follow these steps: Navigate to your project directory : Open your terminal and navigate to the directory containing your Flask app. cd path/to/flask_app Start your Flask app with PM2 : Use the following command to start your app with PM2. You need to specify the Python interpreter and the path to your app.py file. pm2 start app.py --interpreter python3 --name flask_app Replace python3 with your specific Python version if needed. Configure PM2 to run on startup (optional but recommended): To ensure PM2 restarts your app after a server reboot, you can set it up to run on startup with: pm2 startup Follow the instructions that PM2 provides after running this command. Save the PM2 process list : Save the current process list so that PM2 can restore it on reboot: pm2 save Check the status of your app : You can check the status of your Flask app using: pm2 list View logs : To see the logs for your Flask app, you can use: pm2 logs flask_app Now your...

Spring Boot Project with Swagger

1. Setting Up Your Spring Boot Project Step 1: Use Spring Initializr Go to Spring Initializr . Select your project metadata: Project : Maven Project Language : Java Spring Boot : (Select the latest stable version) Group : com.example Artifact : swagger-demo Dependencies : Add Spring Web and Springfox Swagger UI . Click on Generate to download the project ZIP file. Extract it to your preferred location. Step 2: Import the Project into Your IDE Open your favorite IDE (e.g., IntelliJ IDEA, Eclipse). Import the project as a Maven project. 2. Creating a Simple REST API Step 1: Create a Model Class Create a new package com.example.swaggerdemoproject.model and create a class named User . package com.example.swaggerdemoproject.model; public class User { private Long id; private String name; // Getters and Setters public Long getId () { return id; } public void setId (Long id) { this .id = id; } public String getName () { ...

Handling Performance Bottlenecks in Spring Applications

Problem: Many developers encounter performance bottlenecks in their Spring applications due to inefficient database queries, improper caching, and suboptimal resource management. These bottlenecks can lead to slow response times and a poor user experience. Solution: This blog post explores strategies for identifying and resolving performance bottlenecks in Spring applications. 1. Profiling and Monitoring We can use tools like Spring Boot Actuator, VisualVM, or JProfiler for profiling Spring applications. Sample : Example configuration to enable Spring Boot Actuator: @SpringBootApplication public class MyApplication { public static void main (String[] args) { SpringApplication.run(MyApplication.class, args); } } 2. Optimizing Database Queries W riting efficient SQL queries and using pagination to handle large datasets. Sample : Example of using pagination with Spring Data JPA: @Repository public interface UserRepository extends JpaRepository <User, Lon...

Advanced Caching Techniques in Spring

Caching is an essential technique for improving application performance by storing frequently accessed data. Spring Framework offers a robust caching abstraction that simplifies cache management. In this post, we'll explore advanced caching techniques in Spring, including cacheable methods, cache eviction, and cache management. 1. Setting Up Caching in Spring To get started, ensure you have the following dependencies in your pom.xml (for Maven users): < dependency > < groupId > org.springframework.boot </ groupId > < artifactId > spring-boot-starter-cache </ artifactId > </ dependency > < dependency > < groupId > org.springframework.boot </ groupId > < artifactId > spring-boot-starter-data-jpa </ artifactId > </ dependency > < dependency > < groupId > org.h2database </ groupId > < artifactId > h2 </ artifactId > < scope > runtime ...

Retry in SprinBoot Application

In a Spring Boot application, you can use the @Retryable annotation to automatically retry a failed operation. The @Retryable annotation can be applied to a method and can be used to specify the conditions under which the method should be retried. For example: @Retryable (value = { MyException. class }, maxAttempts = 3 , backoff = @Backoff (delay = 1000 )) public void myMethod () throws MyException { // method body } In this example, the myMethod() method will be retried if it throws an exception of type MyException. The method will be retried up to a maximum of 3 times, with a delay of 1000 milliseconds between each retry. You can also specify the @Recover annotation on a method to provide a recovery action for a failed operation. The @Recover method should have the same signature as the retryable method, except that it should have an additional parameter of type Throwable to receive the exception that caused the failure. For example: @Recover public void recover (MyE...

How to cache data in Springboot application

To cache data in a Spring Boot application, you can use the @Cacheable annotation on a method. When the method is called, the data returned by the method will be stored in a cache. Subsequent calls to the method will return the cached data, rather than executing the method again, until the cache is evicted or expired. In the following example,  @EnableCaching  annotation enables the cache mechanism. @SpringBootApplication @EnableCaching public class SpringBootCachingApplication { public static void main (String[] args) { SpringApplication. run (SpringBootCachingApplication. class , args); } } Here is an example of how to use the @Cacheable annotation: @Cacheable (value = "users" , key = "#userId" ) public User getUserById (Long userId) { // This method will be called only on the first invocation, // and the result will be cached. return userRepository. findById (userId); } In this example, the getUserById() method will...

How to create branch from remote branch

git command to create a new local branch from the remote branch git remote add <name> <remote_repo> git fetch <name> then, git checkout -b [branch] [remotename]/[branch]