Archive

Archive for the ‘Java’ Category

When should you consider using ESB


In today’s world, application development is mostly about application reuse and integration than green field application development. Given the volume of IT assets an organization has and the amount of those exposed as services or APIs to be reused over the past decade or so, thanks to SOA and Web Services initiatives, integration is a very common case.

Therefore, whenever you want to consider any project, you need to think about the integration and reuse of services and APIs with the ESB pattern. Thus, the realities of modern day application development would be that:

  • You are integrating three or more services
  • You will have to leave provision for incremental plugging in of applications in the future
  • You will have to support more than one message format or media type
  • You will have to connect and consume services using multiple communication protocols
  • You will need to deal with in-flight message modifications and pick destinations to route messages based on content
  • You will need to expose your application as services or APIs to be consumed by other applications

Hence, you need an ESB because an ESB comes packed with features to cater to these requirements. ESB provides you with a solution development model that ensures your development team adheres to ESB best practices when realizing your solution.

Advertisements
Categories: Java Tags: , ,

Define an output package for Spring Boot application with Assembly plugin


 

Add assembly maven plugin


		${project.artifactId}
		
			
				org.springframework.boot
				spring-boot-maven-plugin
				
					
						
							repackage
						
					
				
			
			
				maven-assembly-plugin
				
					true
					
						${project.basedir}/src/assembly/assembly.xml
					
				
				
					
						create-archive
						package
						
							single
						
					
				
			
		
	

Read more…

Categories: Java Tags: ,

Notions in Apache Kafka

April 23, 2018 Leave a comment

Producer

Producers create new messages. In other publish/subscribe systems, these may be called publishers or writers

Producer concern flowing things:

  • Generate the message (what to send ?)
  • Serialize the message to Kafka format (what is the format of data ?)
  • Send to topic and partitions (to where the message are sent ? )

Consumer

Consumers read messages. In other publish/subscribe systems, these clients may be called subscribers or readers.

Kafka doesn’t track the acknowledgment from consumers the way many JMS queues do.

To consume message from Kafka we need to specify following parameters:

  • Topic name
  • One broker

Consumer Group

Each topic partition is only consumed by one consumer of a group

Number of consumer cannot be larger than number of partition

Partition Rebalance

Poll Loop

Handle following actions:

  • Coordination
  • Partition rebalance
  • Hearth beat
  • Data fetching

Commit

The action of updating current position in the partition is commit

 

Categories: Java Tags: ,

Spring Data JPA with multiple datasource


Requisition

  • Spring Boot 1.5.11.RELEASE
  • Spring Data JPA 1.11.11.RELEASE
  • Hibernate 5.3.5.Final
  • Postgres 9.4

When using Spring Data JPA together with Spring Boot. It’s easily to realize that Spring Boot JDBC help to initialize one primary data source.

But in reality, one application usually need more than one data source. Especially with legacy systems, this kind of system is usually a centralized of all business logic implementations from customer. For instance,  We consider following scenario:

spring-data-jpa-multiple-datasource

Management System  connects to both Office department database and  People department

The following steps are sufficient for you to connect to one additional data source:

Exclude the default initalized data source from Spring Boot JDBC

@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
public class Application {

    public static void main(String[] args) {
        SpringApplication
            .run(Application.class, args);
    }
}

Configure data source Office

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "officeEntityManagerFactory", transactionManagerRef = "officeTransactionManager", basePackageClasses = {
    DeviceRepository.class })
public class OfficeDataSourceConfiguration {

    @Bean(name = "officeEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(
        final EntityManagerFactoryBuilder builder,
        @Qualifier("officeDataSource") final DataSource dataSource) {
        return builder.dataSource(dataSource).packages(Device.class)
            .persistenceUnit("office").build();
    }

    @Bean(name = "officeDataSource")
    @ConfigurationProperties(prefix = "datasource.office")
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "officeTransactionManager")
    public PlatformTransactionManager transactionManager(
        @Qualifier("officeEntityManagerFactory") final EntityManagerFactory entityManagerFactory) {
        return new JpaTransactionManager(entityManagerFactory);
    }
}

Configure data source People

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "peopleEntityManagerFactory", transactionManagerRef = "peopleTransactionManager", basePackageClasses = {
    DepartmentRepository.class })
public class PeopleDataSourceConfiguration {

    @Primary
    @Bean(name = "peopleEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(
        final EntityManagerFactoryBuilder builder,
        @Qualifier("peopleDataSource") final DataSource dataSource) {
        return builder.dataSource(dataSource).packages(Department.class)
            .persistenceUnit("people").build();
    }

    @Primary
    @Bean(name = "peopleDataSource")
    @ConfigurationProperties(prefix = "datasource.people")
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }

    @Primary
    @Bean(name = "peopleTransactionManager")
    public PlatformTransactionManager peopleTransactionManager(
        @Qualifier("peopleEntityManagerFactory") final EntityManagerFactory entityManagerFactory) {
        return new JpaTransactionManager(entityManagerFactory);
    }
}

Update configuration in application.yml

spring:
    application:
        name: spring-data-jpa-multiple-datasource
    profiles:
        active: default
    data.jpa.repositories.enabled: true
    jpa:
        database-platform: POSTGRESQL
        generate-ddl: true
        open-in-view: true
        show-sql: false
        hibernate.ddl-auto: update
        properties:
            hibernate:
                dialect: org.hibernate.dialect.PostgreSQL94Dialect
                default_schema: production

datasource:
    people:
        driverClassName: org.postgresql.Driver
        url: jdbc:postgresql://10.10.15.171:5432/people?currentSchema=production
        username: postgres
        password: postgres
        type: org.apache.tomcat.jdbc.pool.DataSource
        jmx-enabled: true
        initialSize: 2
        maxActive: 100
        maxIdle: 5
        minIdle: 2
        maxWait: 600000
        testOnBorrow: true
        validationQuery: select 1
        minEvictableIdleTimeMillis: 60000
        removeAbandoned: true
        removeAbandonedTimeout: 60000
        testWhileIdle: true
        timeBetweenEvictionRunsMillis: 60000

    office:
        driverClassName: org.postgresql.Driver
        url: jdbc:postgresql://10.10.15.171:5432/office?currentSchema=production
        username: postgres
        password: postgres
        type: org.apache.tomcat.jdbc.pool.DataSource
        jmx-enabled: true
        initialSize: 2
        maxActive: 100
        maxIdle: 5
        minIdle: 2
        maxWait: 600000
        testOnBorrow: true
        validationQuery: select 1
        minEvictableIdleTimeMillis: 60000
        removeAbandoned: true
        removeAbandonedTimeout: 60000
        testWhileIdle: true
        timeBetweenEvictionRunsMillis: 60000

Source code sample

Configuration ngoài cho ứng dụng với Spring Boot

June 12, 2015 2 comments

1. Sử dụng properties file SpringApplication sẽ load các property từ tất cả các file application.properties trong các vị trí sau, và thêm chúng vào application environment: + Folder /config bên trong folder hiện tại. + Folder hiện tại. + Class path /config + Root class path Mặc định, SpringApplication sẽ load theo thứ tự từ dưới lên trên. Và các property nào load sau sẽ override property load trước. Vì lý do này, bạn cứ đặt property vào class path bình thường, khi muốn config ngoài file jar, thì sẽ đặt file application.properties vào folder /config là được. Như vậy cần phải build dự án với maven sao cho có kết quả trong folder target như sau:

+-target
    +---myapp.jar
    +---config
            +--application.properties

Read more…

Categories: Java Tags: , ,

Sử dụng configuration class trong Spring Boot


1. Sử dụng class Configuration
Spring Boot ưu ái việc sử dụng các class Configuration. Mặc dù bạn có thể hoàn toàn sử dụng việc gọi hàm SpringApplication.run() ngay bên trong file XML, nhưng nhìn chung Spring khuyên điểm xuất phát của ứng dụng là một class với annotation @Configuration. Thông thường thì class với hàm main() thì nên là cũng là lớp chứa @Configuration.

import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.Configuration;

@Configuration
public class Application
{
    public static void main(final String[] args)
    {
        SpringApplication.run(Application.class, args);
    }
}

Read more…

Categories: Java Tags: , , ,

Extract content and attachments from an Message use Javax mail (with embedded message EML, MSG)


The main part in a javax message is the content. The content can be any Java object representing the mail content, like a plain text (String) or raw image data or even a multiple part object. A multiple part content a message’s content consists of more than a single item. A multiple part object is a container which holds one ore more BodyPart objects. These BodyParts, like a message, have a content and a content type (in fact, both message and BodyPart implement the same interface Part which carries these properties).

Beside plain content, A BodyPart can contain another Multipart or even another Message, a so called nested message (e.g. a forwarded message, called embedded message) with content type ‘message/rfc822’.

image1-6

As you can see, the structure of a message can be rather heterogeneous, a tree with nodes of different types. The following picture illustrates the tree structure for a sample message.
Read more…

Categories: Java Tags: , , ,
%d bloggers like this: