Sample application: https://github.com/microservices-patterns/ftgo-application http://eventuate.io/exampleapps.html Microservices Patterns: https://microservices.io/patterns/index.html Enterprise Integration Patterns: https://www.enterpriseintegrationpatterns.com/patterns/messaging/ Configuration Management Centralizing configuration and secret values: AWS Secrets Manager AWS Key Management Service Apache ZooKeeper Spring Cloud Config (Spring Cloud Vault, Spring Cloud Zookeeper) etcd – distributed reliable key-value store Service Discovery Client make request to Service Registry or Load Balancer (Router): Apache […]
Author Archives: rows
PostgreSQL quick start
How to install PostgreSQL with brew:
1 2 |
$ brew search postgresql $ brew install postgresql |
add to startup:
1 |
$ ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents |
start:
1 |
brew services start postgresql |
or manually:
1 |
postgres -D /usr/local/var/postgres |
with ctl:
1 |
pg_ctl -D /usr/local/var/postgres start |
check status:
1 |
pg_ctl -D /usr/local/var/postgres status |
port:
1 |
egrep 'listen|port' /usr/local/var/postgres/postgresql.conf |
logs:
1 |
/usr/local/var/log/postgres.log |
How to setup PostgreSQL
1 2 3 |
$ postgres -V postgres (PostgreSQL) 10.5 |
1 |
psql -l |
1 2 3 4 5 6 7 8 |
Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+---------------+----------+-------------+-------------+------------------------------------- postgres | username | UTF8 | en_US.UTF-8 | en_US.UTF-8 | template0 | username | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/"username" + | | | | | "username"=CTc/"username" template1 | username | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/"username" + | | | | | "username"=CTc/"username" (3 rows) |
1 2 3 4 5 |
$ psql postgres psql (10.5) Type "help" for help. postgres=# |
1 2 3 4 5 |
postgres=# \du List of roles Role name | Attributes | Member of ---------------+------------------------------------------------------------+----------- username | Superuser, Create role, Create DB, Replication, Bypass RLS | {} |
quit psql:
1 |
\q |
create application user and database
1 2 |
$ createuser appname $ createdb -O appname appname |
setup password for user:
1 |
postgres=# \password username |
Enjoy!
React or Vue
Initialization List with values in one row in Java
Java 9: List.of(“a”, “b”, “c”); Set.of(“a”, “b”, “c”); Java 8: Stream.of(“a”, “b”, “c”).collect(Collectors.toList()); Stream.of(“a”, “b”, “c”).collect(Collectors.toCollection(ArrayList::new)); Older: Collections.singletonList(); Arrays.asList(); new ArrayList() {{ add(“a”); add(“b”); add(“c”); }};
Compiling!!!
Mockito, Spy, PowerMock
Mockito
1 2 |
Mockito.mock(externalService); Mockito.reset(externalService); |
1 |
when(mock).thenReturn(value); |
1 |
when(mock).thenThrow(new IOException()); |
1 |
doReturn(result).when(mock_object).void_method_call(); |
1 |
doThrow(new IOException()).when(mock_object).void_method_call(); |
1 |
Mockito.verify(mock_object).callMethod(Matchers.eq(concrete_object)); |
Mockito Mocks vs. Spies Mock – bare-bones shell instance
1 |
List mockedList = Mockito.mock(ArrayList.class); |
Spy – wrap an existing instance
1 |
List spyList = Mockito.spy(new ArrayList()); |
PowerMock final classes without interfaces static methods privates
1 2 |
@RunWith(PowerMockRunner.class) @PrepareForTest({ExternalService.class}) |
1 |
private final ExternalService externalService = PowerMockito.mock(ExternalService.class); |
1 2 |
PowerMockito.mockStatic(StaticService.class); PowerMockito.verifyStatic(); |
Test-driven development: JUnit
gradle:
1 2 3 4 5 |
apply plugin: 'java' dependencies { testCompile 'junit:junit:4.12' } |
maven:
1 2 3 4 5 |
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> |
Expected Exceptions Test
1 2 3 4 |
@Test(expected = UnsupportedOperationException.class) public void reverseList() { list.getReverseList(); } |
@RunWith BlockJUnit4ClassRunner – default junit runner SpringJUnit4ClassRunner allows you to use dependency injection in test classes or to create transactional test methods. MockitoJUnitRunner for automatic mock initialization. Suite Parameterized Parameters class
1 2 3 4 5 6 7 8 9 |
@RunWith(value = Parameterized.class) public class ParametersTest { // ... @Parameters public static Collection<Object[]> data() { @Test //will be run Collection.size times public void testData(){ } |
JUnit 5 – @ExtendWith
1 |
@RunWith(SpringJUnit4ClassRunner.class) |
replace with
1 |
@ExtendWith(SpringExtension.class) |
@Rule
1 |
@Rule public ExpectedException exception = ExpectedException.none(); |
1 |
@Rule public TemporaryFolder folder = new TemporaryFolder(); |
or your custom rule: […]
Hibernate – OneToOne, OneToMany, ManyToOne, ManyToMany mappings
By specifying the @JoinColumn on both models you don’t have a two way relationship. mappedby One to One
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
@Entity(name = "PostDetails") @Table(name = "post_details") public class PostDetails { @OneToOne(fetch = FetchType.LAZY) @JoinColumn(name = "post_id") private Post post; } @Entity(name = "Post") @Table(name = "post") public class Post { @OneToOne(mappedBy = "post", cascade = CascadeType.ALL, fetch = FetchType.LAZY, optional = false) private PostDetails details; } |
bidirectional One to One is EAGER @MapsId (before jpa2 – @PrimaryKeyJoinColumn)
1 2 3 4 5 6 7 |
@Entity(name = "PostDetails") @Table(name = "post_details") public class PostDetails { @OneToOne(fetch = FetchType.LAZY) @MapsId private Post post; } |
One To Many, Many To One Unidirectional without @JoinColumn requires additional table for joining
1 2 3 4 5 6 7 8 9 |
@Entity(name = "Post") @Table(name = "post") public class Post { @OneToMany( cascade = CascadeType.ALL, orphanRemoval = true ) private List<PostComment> comments = new ArrayList<>(); } |
1 2 3 |
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true) @JoinColumn(name = "post_id") private List<PostComment> comments = new ArrayList<>(); |
Bidirectional
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
@Entity(name = "Post") @Table(name = "post") public class Post { @OneToMany( mappedBy = "post", cascade = CascadeType.ALL, orphanRemoval = true ) private List<PostComment> comments = new ArrayList<>(); } @Entity(name = "PostComment") @Table(name = "post_comment") public class PostComment { @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "post_id") private Post post; |
Is it possible to limit the […]
Hibernate – JPA
1 2 3 4 5 |
<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.2.17.Final</version> </dependency> |
SessionFactory (jpa: EntityManagerFactory) – thread-safe (and immutable) representation of the mapping of the application domain model to a database Session (jpa: EntityManager) – A single-threaded, short-lived object conceptually modeling a “Unit of Work” StatelessSession – like JDBC Transaction (jpa: EntityTransaction) – A single-threaded, short-lived object used by the application to demarcate individual physical transaction […]
Spring Bean Life Cycle
By implementing InitializingBean and DisposableBean interfaces Providing init-method and destroy-method in xml configuration or @PostConstruct and @PreDestroy annotations for methods
1 |
@Bean(initMethodName="init") |
1 |
<bean id="service" class="Service" init-method="init" destroy-method="destroy"> |
For activating post processor annotation annotations use
1 |
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" /> |
or by
1 |
<context:annotation-config> |
used to activate applied annotations in already registered beans in application context (context:component-scan does what context:annotation-config does, but additionally it scan the […]