Sunday, 10 December 2017

Implement Spring Retry in Spring Boot

<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjweaver</artifactId>
  <version>1.8.8</version>

</dependency>


<dependency>
  <groupId>org.springframework.retry</groupId>
  <artifactId>spring-retry</artifactId>
  <version>1.1.5.RELEASE</version>
</dependency>



@EnableRetry
@SpringBootApplication
@ComponentScan(basePackages="com.example")
@EnableAutoConfiguration
public class SampleApplication extends SpringBootServletInitializer{

///.....
}


@Service
public class MyService {

@Retryable(value = {
IndexOutOfBoundsException.class, Exception.class}, maxAttempts = 10, backoff = @Backoff(delay = 5000))
  public void retryWithException() {
System.out.println("running......");
    List<Integer> list  = new ArrayList<>();
    list.get(3);
   

  }


@RequestMapping(value="/testRetry",method=RequestMethod.GET)
public @ResponseBody String retry(){
System.out.println("requestReceived::"+"/retry");
myservice.retryWithException();
return "done";
}



output:

requestReceived::/retry
running......
running......
running......
running......
running......
running......
running......
running......
running......
running......
2017-12-10 19:24:26.924 ERROR 30540 --- [io-8080-exec-17] o.s.boot.web.support.ErrorPageFilter     : Forwarding to error page from request [/testRetry] due to exception [Index: 3, Size: 0]

java.lang.IndexOutOfBoundsException: Index: 3, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:653) ~[na:1.8.0_144]
at java.util.ArrayList.get(ArrayList.java:429) ~[na:1.8.0_144]





//recover after retry

//add below in MyService class

 @Recover
  public void recover(IndexOutOfBoundsException exception) {
          System.out.println("ah... catch");

  }



output:


requestReceived::/retry
running......
running......
running......
running......
running......
running......
running......
running......
running......
running......
ah... catch


Sunday, 12 November 2017

Saturday, 11 November 2017

Create Spring Boot Sample Project

You can download a running  sample of spring boot on below link:

https://github.com/Sushil21/SpringBootSample

Add Image Water Mark on Image in JAVA

Below code for add image water mark on image in java:


import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;

public class TestImage41im {

public static void main(String[] args) {
try {
File sourceImageFile = new File("originalImagePath/originalimage.jpg");
File destImageFile = new File("destinationImagePath/destinationImage.png");
File watermarkImageFile = new File("waterMarkImagePath/watermark.png");

BufferedImage sourceImage = ImageIO.read(sourceImageFile);
BufferedImage watermarkImage = ImageIO.read(watermarkImageFile);

// graphic properties
Graphics2D g2d = (Graphics2D) sourceImage.getGraphics();
AlphaComposite alphaChannel = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f); //add transparency of watermark image 
g2d.setComposite(alphaChannel);

//coordinate where the image is painted below are the center
int x = (sourceImage.getWidth() - watermarkImage.getWidth()) / 2;
int y = (sourceImage.getHeight() - watermarkImage.getHeight()) / 2;

// draw the image watermark
g2d.drawImage(watermarkImage, x, y, null);
ImageIO.write(sourceImage, "png", destImageFile);
g2d.dispose();
System.out.println("The image watermark is added to the image.");
}catch (Exception e) {
// TODO: handle exception
}
}

}

Add Text Water mark on Image in JAVA

Below code for add text water mark on image in JAVA:

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;

import com.itextpdf.awt.geom.Dimension;



public class TextWaterMarkOnImage {

public static void main(String[] args) {
try {
File sourceImageFile = new File("originalFilePath/original.jpg");
File destImageFile = new File("watermarkedFilePath/watermarked.png");
BufferedImage sourceImage = ImageIO.read(sourceImageFile);
Graphics2D g2d = (Graphics2D) sourceImage.getGraphics();
AlphaComposite alphaChannel = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f); //set transparency of watermark
g2d.setComposite(alphaChannel);
g2d.setColor(Color.BLACK);
Font font = new Font("Arial", Font.BOLD, 64); //font of text
g2d.setFont(font);
String text = "Checked"; //watermark text
Dimension dmDimension = getDimentionOfText(g2d, font, text);
int centerX  = sourceImage.getWidth()/2 - (int)dmDimension.getWidth()/2;
int centerY = sourceImage.getHeight()/2 + (int)dmDimension.getHeight()/2;
// paints the textual watermark
g2d.drawString(text, centerX, centerY);
ImageIO.write(sourceImage, "png", destImageFile);
g2d.dispose();
System.out.println("The text watermark is added to the image.");
}catch (Exception e) {
// TODO: handle exception
}
}

public static Dimension getDimentionOfText(Graphics2D g2d, Font font , String text) {
// get metrics from the graphics
FontMetrics metrics = g2d.getFontMetrics(font);
// get the height of a line of text in this
// font and render context
int hgt = metrics.getHeight();
// get the advance of my text in this font
// and render context
int adv = metrics.stringWidth(text);
// calculate the size of a box to hold the
// text with some padding.
Dimension size = new Dimension(adv+2, hgt+2);
System.out.println(size.getHeight() +"  "+ size.getWidth());
return size;
}

}

Add image water mark on PDF in JAVA using iText

Below code for add image water mark on PDF using iText:

import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfGState;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;

public class ImageWaterMarkOnPdf {
public static void main(String... args)   {

try {
PdfReader reader = new PdfReader("originalfilepath/otiginal.pdf");
PdfStamper stamper = new PdfStamper(reader,
new FileOutputStream("outputfilepath/output.pdf"));

// image watermark
Image img = Image.getInstance("watermarkfilepath/watermark.png");
float w = img.getScaledWidth();
float h = img.getScaledHeight();

// properties
PdfContentByte over;
Rectangle pagesize;
float x, y;

// add water mark on all pages
int n = reader.getNumberOfPages();
for (int i = 1; i <= n; i++) {

// get page size and position for add water mark
pagesize = reader.getPageSizeWithRotation(i);
x = (pagesize.getLeft() + pagesize.getRight()) / 2;
y = (pagesize.getTop() + pagesize.getBottom()) / 2;
over = stamper.getOverContent(i);
over.saveState();

// set transparency
PdfGState state = new PdfGState();
state.setFillOpacity(0.2f); //more transparent
over.setGState(state);

// add image water mark center
over.addImage(img, w, 0, 0, h, x - (w / 2), y - (h / 2));
over.restoreState();
}
stamper.close();
reader.close();
}catch (IOException e) {
// TODO: handle exception
}catch (DocumentException e) {
// TODO: handle exception
}
 
}


}

Get Bytes from ByteArrayOutputStream

Below code for get byte array from ByteArrayOutputStream:


byte[] waterMarkedBytes = baos.toByteArray();

links for Data Structure

  1) 𝐁𝐞𝐜𝐨𝐦𝐞 𝐌𝐚𝐬𝐭𝐞𝐫 𝐢𝐧 𝐋𝐢𝐧𝐤𝐞𝐝 𝐋𝐢𝐬𝐭:  https://lnkd.in/gXQux4zj 2) 𝐀𝐥𝐥 𝐭𝐲𝐩𝐞𝐬 𝐨𝐟 𝐓𝐫𝐞𝐞 𝐓𝐫𝐚𝐯𝐞𝐫𝐬𝐚𝐥𝐬...