As sending an email in Java or C sharp, it needs to connect to an SMTP server to send out the email message.
The Modern secured SMTP server is listening on port 587.
As sending an email in Java or C sharp, it needs to connect to an SMTP server to send out the email message.
The Modern secured SMTP server is listening on port 587.
As the name implies, an anonymous inner class isn’t defined using an explicit name.
An anonymous inner class is created when you combine instance creation with inheriting a class or implementing an interface. Anonymous classes come in handy when you wish to override methods for only a particular instance. They save you from defining new classes.
The anonymous class might override none, few, or all methods of the inherited class. It must implement all methods of an implemented interface. The newly created object can be assigned to any type of variable—static variable, instance variable, local variable, method parameter, or returned from a method. Let’s start with an example of an anonymous inner class that extends a class.
ScheduleExecutorService extends ExecutorService
ExecutorService manages to submit tasks much better than Executor. It accepts both Runnable and Callable.
ScheduleExecutorService manages to submit tasks and run in planned time slices.
'IsTerminated()': testing if all submitted tasks having been accomplished.
'IsShutDown()': testing if the executor is shut down.
'shutDownNow' means that attempting to halt active running threads, and stop picking up tasks from the queue, and returning un-implemented tasks in a collection. 'shutDownNow' is more like shutting down at once.
Executor consists of a queue and a pool.
Pool types: fixed-sized, single thread, scheduled thread pool, cached thread pool
The 'Executors' is a factory, where a client achieve a specific type of thread pool.
ExecutorService es = Executors.newCachedThreadPool();
When a microservice starts up, it goes through several steps in its lifecycles, i.e. Assembly, BootStrapping, Discovery, and Monitoring.
packing all the source code and all its dependencies, together with its runtime engine in a single installable artefact.
It can eliminate the traditional Configuration drift problem, which is due to server configuration and source are managed by different admin team and development team respectively.
It can quickly deploy the micro-service in response to a sudden influx of requests.
@Jsonformat is an annotation belong to package Jackson-databind
It is used on a Getter method to format how the Date instance is serialized. By default, a Date instance is serialized as a number ref. to the begging of the time counting.
As using @JsonFormat, the time output is formatted ref. to a default time-zone or a locale. Normally it is the GMT time zone.
@Getter
@Setter
@AllArgsConstructor
@Builder
public class RoomReservation {
private Long roomId;
private Long guestId;
private String roomName;
private String roomNumber;
private String firstName;
private String lastName;
@Getter(AccessLevel.NONE)
private Date date;
@JsonFormat(pattern = "yyyy-MM-dd", shape = JsonFormat.Shape.STRING, timezone = "Europe/Copenhagen")
public Date getDate() {
return date;
}
}
After compiling, List<T> aList, generic type parameter will be erased and replaced by an Object type. That is a common Java generic operation. However, for a deserialization process, an Object type instance is too general to re-build. You may solve this by using Array or ParameterizedTypeReference interface.
using Array instance instead of Collection of Generics
@Test
@Sql("classpath:testdata.sql")
@Sql(value = "classpath:deleteTestData.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
void testFindListCustomerByArray() {
URI uri = builder.build().toUri();
ResponseEntity<CustomerDto[]> response = this.template.getForEntity(uri, CustomerDto[].class);
CustomerDto[] customerDtoList = response.getBody();
CustomerDto[] emptyArray = {};
List<String> firstNames = Arrays.asList("Mike", "Mia");
boolean matched = Arrays.stream(customerDtoList != null ? customerDtoList : emptyArray)
.map(c -> c.getFirstName()).collect(toList()).equals(firstNames);
assertAll(
() -> assertThat(customerDtoList).hasSize(2),
() -> assertThat(matched).isTrue()
);
}
using ParamterizedTypeReference Interface
@Test
@Sql("classpath:testdata.sql")
@Sql(value = "classpath:deleteTestData.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
void testFindAllCustomers() {
URI uri = builder.build().toUri();
//this.template.getForEntity(uri, CustomerDto[].class);
ResponseEntity<List<CustomerDto>> response = this.template.exchange(uri, HttpMethod.GET, null,
new ParameterizedTypeReference<List<CustomerDto>>() {
});
List<CustomerDto> customerDtoList = response.getBody();
List<String> firstNames = Arrays.asList("Mike", "Mia");
boolean matched = customerDtoList.stream().map(c -> c.getFirstName()).collect(toList()).equals(firstNames);
assertAll(
() -> assertThat(customerDtoList).hasSize(2),
() -> assertThat(matched).isTrue()
);
}
{"id":1,"firstName":"string","lastName":"string","email":"ynz@hotmail.com","orders"}{"message":"Could not write JSON: failed to lazily initialize a collection of role: com.ynz.demo.springjpatransaction.entities.Customer.orders, could not initialize proxy - no Session; nested exception is com.fasterxml.jackson.databind.JsonMappingException: failed to lazily initialize a collection of role: com.ynz.demo.springjpatransaction.entities.Customer.orders, could not initialize proxy - no Session (through reference chain: com.ynz.demo.springjpatransaction.entities.Customer[\"orders\"])"}
Cause: On the surface, the exception comes from JSON parsing and meeting unexpected char; the fundamental is due to the orders are lazily fetched, but accessed outside a live persistence session. After the entity-manager loading the customer into the persistence context, and then it closed the session; afterwards accessing entity Order become impossible, and cause this exception.
Solution: keeping the persistence session open until the moment accessing the lazily loaded Entities. The optimal solution: using the Fetch join clause to fetch entity root and its aggregated entities by one query. The worst solution: making the aggregate entities become Eager-fetched, this will cause an N+1 problem, and therefore slowing down the system performance.
Yes, but must include JSR310. Thus ZonedDateTime can be deserialized directly from JSON response to POJO field. <dependency> <g...