Posts

Showing posts from November, 2018

Mapping Multiple Primary Keys in Entity Framework

mapping-multiple-primary-keys-in-entity-frameworkThis tip shows one simple solution to allow multiple primary keys mapping in entities.When a datatable has multiple keys, the entity framework by default gets confused for mapping the primary keys. By assigning an Order value to the Column attribute when mapping the primary keys solves this problem. This Order value should be in the same order as these columns appear in the datatable.using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace LeafyJava.Models { [Table("leafyjava.event")] public class Event { [Key] [Column("pub_id", Order = 0)] public string PubId { get; set; } [Key] [Column("auth_id", Order = 1)] public string AuthId { get; set; } [Column("description")] public string Description { get; set; } } }

Override Enum Methods with Constant Specific Class Body

override-enum-methods-with-constant-specific-class-bodyThis article shows one advanced technique when using enums - constant specific class body.Enum is a specialized class whose instances are created and managed completely by JVM. So we should never invoke constructors of an enum, otherwise there might be a meltdown of the JVM, and fortunately, our loyal friend compiler always help us catching the error before it happens. Most of the time we use enum for two purposes – to create either singleton or a list of predefined enumerable list.Given the following scenario, create an enum of currency that is supported by a vending machine, we might end up with the following enum:publicenum Currency {QUARTER(0.25),ONE_DOLLAR(1),FIVE_DOLLAR(5);privatedouble value;Currency(finaldouble value){this.value = value;}publicdoublegetValue(){return value;}}Now our vending machine also needs to know if the money inserted is a note or a coin, we will need to provide such information in our enum type. e.g. …

How to Setup ELK Stack with Docker for Development

how-to-setup-elk-stack-with-docker-for-developmentThis tip shows the fastest way to stand up the ELK stack for development.The Elastic stack has a nice website that provides the links to the docker images and documentation. In this example, I am using the latest version as this article is written - v6.4.3 for all the elastic stack.1. Run ElasticSearchdocker run -d --name my-es -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:6.4.3 After the container starts running, find out the internal docker IP the ElasticSearch server is using. This will be used for configuring the Logstash and Kibana.docker inspect -f '{{ .NetworkSettings.IPAddress }}' my-es 2. Run LogstashCreate a configuration file in the host directory, and bind mount to the container configuration file.vim ~/elk/config/logstash.conf Enter the following and save with :wq. Note that we created an index pattern that starts with leafyjava-.input { tcp {…

Reload Configurations from Spring Cloud Config

reload-configurations-from-spring-cloud-configSpring Cloud Config Server automatically retrieves specified configurations backed on Git based Repository. To ask the affected Spring Cloud Config clients to use the updated configuration, a signal needs to be sent to the client. This article shows two approaches.One to One ApproachWith Spring Actuator project, we get a series of utility tools for administering the server. One of them is /refresh, upon calling this endpoint, if the application context is annotated with @RefreshScope, it will be signaled to refresh. This endpoint is called on the client application.Add the following dependency to the client application, and annotate the application with @RefreshScope.<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency>For instance, given the configuration server is on localhost:8888 and the configuration client is on localhost:8081, to …

WebMethods Implicit and Explicit Transaction Management

webmethods-implicit-and-explicit-transaction-managementIt is not documented from any of the WebMethods resources that how to manage transactions implicitly and explicitly. This tip shows the missing piece that will prevent a transaction holding a connection until the transaction owner thread is killed.Imagine a case you have to do the following operations:Execute a stored procedure that takes a data table as input parameters. (ExecuteService adapter)Query a table (Select adapter)Update the table A (Update adapter)Update the table B (Update adapter)step 2-4 must be realized in together.With a typical Java workflow, a transaction will be wrapped around the step 2-4 to make sure when any of the steps fail, the changes made within this transaction gets rolled back.In WebMethods, JDBC connections are managed by a connection pool. When an adapter service is run, if the adapter service is created from the same connection, then it will attempt to acquire the connection from the pool. If there…

Serialize Timestamp To EpochSecond with Jackson

serialize-timestamp-to-epochsecond-with-jacksonSome REST API only takes a long as its Date field, such as esriFieldTypeDate from ArcGIS Server Rest API. But with the rich functionality, the Java 8 time API provides, it would be silly to use long as the type for our object modeling. This tip shows how to solve this problem.I started with using LocalDateTime as the type of the timestamp field I want to use`, but Jackson simply serialize it as it does with any other fields, but what I really want is to serialize it to the epoch second.Jackson does provide a new library that supports the JSR 310 which is the date and time API. to use that dependency, we need to add the following to maven:<dependency> <groupId>com.fasterxml.jackson.datatype</groupId> <artifactId>jackson-datatype-jsr310</artifactId> </dependency> At this point, the job is not done yet. The LocalDateTime is not an option because it takes a timezone information to convert to the epo…

Pass Parameters to the ExecutiveService Adapter

pass-parameters-to-the-executiveservice-adapterThis tip shows how to pass pipeline input variables to an ExecutiveService adapter in webMethods.The ExecutiveService adapter is used to execute a java service which utilizes the connection from the Integration Server managed connection pool to execute custom SQL statements. The adapter service input include an Object type targetServiceInput. This actually is a IS document type based on my experiment.To properly send pipeline variables to the wrapped java service, a wrapper document type needs to be created in a map service to map to the targetServiceInput object in the adapter service and then any properties under this document, will be available to access from the wrapper java service by doing the following:IDataMap idm =newIDataMap(pipeline); String foo = idm.getAsString("bar");This java service has an input of bar defined in its Input tab.

Docker Useful Commands

docker-useful-commandsThis article shows a curated list of docker commands that may be useful.1. Inspect container and filter by its propertydocker inspect -f '{{ .NetworkSettings.IPAddress }}' container_name 2. detached and interactive terminalRun a nginx container in a detached interactive terminal that maps the host port 8088 to the nginx webserver port
and open a shell in the interactive terminal.docker run -dit --name my-nginx -p 8088:80 nginx /bin/bash Enter the shelldocker attach my-nginx Exit the shell while keeping the nginx runningCtl+P, Ctl+Q