Posts

write huge text into clob column in oracle

 varchar2 max character size is 32767 , that's why writing huge texts into column is problem. CLOB type is used for this, it can store 2,147,483,647 characters. But it is a bit problematic to write more than 32767 characters into CLOB typed column. For doing that either we should read from file or divide into small chunks. In example we will do this by chunks Firstly: package org.example; import java.io.*; import java.util.ArrayList; import java.util.List; public class FileChunker { private static final int CHUNK_SIZE = 21000; // Size of each chunk public static void main(String[] args) { String filePath = "/Users/bbb/Documents/aaa/new_license/license_en.html"; // Path to your file String outputDir = "/Users/bbb/Documents/aaa/new_license/chunks_en"; // Directory to save chunks try { List<String> chunks = splitFileIntoChunks (filePath); createFilesFromChunks (chunks, outputDir); System. out...

entity id sequence la generate olan zaman hibernate in bazaya select gondermesi

hibernate, entity id uchun sequence istifade ederken @GeneratedValue( generator = "USERS_ID_SEQ_GEN", strategy = GenerationType.SEQUENCE) @SequenceGenerator( name = "USERS_ID_SEQ_GEN", sequenceName = "USERS_ID_SEQ", allocationSize = 1)  her save etmezden qabag bazaya sequencein novbeti valuesunu oyrenmek uchun sorgu atir bu selectin sayini azaltmaq uchun allocationSize -i boyuk eded vermek olar. Onda her eded tamamlandiqda sorgu gonderilib novbeti tsikl oyrenilecek. Bunun menfisi odur ki, app restart olanda yeni save olunacaq entitylerin idsi bazadaki sonuncu id + allocation size a beraber olur. Yeni sonuncu id = 5 olanda restart gedibse , eger allocation = 50 dirse , onda yeniler 55den bashlayacaq.  selecti umumyetle legv etmek uchun, @GeneratedValue(strategy = GenerationType.IDENTITY) istifade ede bilerik. Bu zaman hibernate save olunacaq entitynin idsini bilmir ve id bazada yaradilir. Bunun menfisi odur ki, bulk i...

Inject private field from unit test

public class DeeplinkService {  @Value("${deeplink}")  private String deeplink; } class DeeplinkServiceTest { @InjectMocks private DeeplinkService deeplinkService; @BeforeEach  public void setup() { { ReflectionTestUtils.setField( deeplinkService, "deeplink", TestConstants.TEST_DEEPLINK ); } } It is possible to inject private field from unit test with Reflection

libfontmanager.so: libfreetype.so.6 not found error in docker image when trying to generate excel with Apache Poi

  FROM openjdk: 11.0.3 - jdk-slim-stretch imageinde  libfontmanager.so: libfreetype.so.6 bu lib olmur ona gorede. ,apache poi ile excel generate etmeye chalishanda xeta aliriq Xetanin ozu:  "java.lang.UnsatisfiedLinkError: /usr/local/openjdk-11/lib/libfontmanager.so: libfreetype.so.6: cannot open shared object file: No such file or directory Hell etmek uchun Dockerfile a bu stepi elave etmek lazimdir RUN set -eux; \ apt-get update; \ apt-get install -y --no-install-recommends fontconfig libfreetype6; \ rm -rf /var/lib/apt/lists/* Menbe: https://hub.docker.com/r/hydrz/openjdk/dockerfile

Testing mock with Clock object for LocalDateTime now

 LocalDateTime.now() will create problem when unit testing, for this purpose using help of Clock object is recommended way. Let's see example: @Service @RequiredArgsConstructor @Slf4j public class OrdersServiceImpl implements OrdersService { private final OrdersRepository ordersRepository ; private final OrdersDeliveryEventsRepository ordersDeliveryEventsRepository ; private final MailService mailService ; private final Clock clock = Clock. systemDefaultZone () ; private final FileGenerator fileGenerator ; @Override @Scheduled (cron = "${cron.expression}" ) @SchedulerLock (name = "TaskScheduler_processOrders" , lockAtMostFor = "15M" , lockAtLeastFor = "2M" ) public void processOrders () throws IOException { log .info( "processOrders started" ) ; List<OrdersEntity> unsentOrders = ordersRepository .getUnsentOrders(LocalDateTime. now ( clock ).minusDa...

Jackson Date Serialize and Deserialize with UTC+0 format and UTC+4

public class JacksonConfig { private static final String DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss" ; public static class CustomLocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> { private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter. ofPattern ( DATE_TIME_FORMAT ) ; @Override public Class<LocalDateTime> handledType () { return LocalDateTime. class; } @Override public LocalDateTime deserialize (JsonParser parser , DeserializationContext ctxt) throws IOException { LocalDateTime dateTime = LocalDateTime. parse (parser.getText() , DATE_TIME_FORMATTER ) ; return dateTime.atZone(ZoneId. of ( "UTC" )) .withZoneSameInstant(ZoneId. systemDefault ()).toLocalDateTime() ; } } public static class CustomLocalDateTimeSerializer extends JsonSerializer<LocalDate...

AES encrypt-decrypt in Java

 public static String decrypt(byte[] data, byte[] key) throws NoSuchPaddingException, NoSuchAlgorithmException,             BadPaddingException, IllegalBlockSizeException, InvalidKeyException {         Cipher aes = Cipher.getInstance("AES/ECB/PKCS5Padding");         aes.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key,     "AES"));         return new String(Base64.getDecoder().decode(Base64.getEncoder().encodeToString((aes.doFinal(data))))); } public static String encrypt(byte[] data, byte[] key) throws NoSuchPaddingException, NoSuchAlgorithmException,             BadPaddingException, IllegalBlockSizeException, InvalidKeyException {         Cipher aes = Cipher.getInstance("AES/ECB/PKCS5Padding");         aes.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key,     "AES"));         return Ba...