跳至主要内容

博文

目前显示的是 五月, 2017的博文

RabbitMQ Learning (1): Basic

This blog is the learning note of RabbitMQ, would like to share here and learn with all of we. Basic Concepts Channel Channel is used by rabbit client to communicate with server. The channel is backed by TCP, which can increase the reliability without extra effort. Besides that, this abstraction layer, channel layer, over TCP connection, can be used to reuse same connection and provide privacy at the same time. Queue Config A message queue has following common settings: name: used by client to refer server queue exclusive: limit only one consumer for this queue auto-delete: last consumer remove subscription, the queue is deleted Queue Creation Queue is located in the server of rabbit, what we have is just a stub. If we just restart our clients, queue will not be affected. consumer creation: we can endure the miss of producer’s message producer & consumer: we can’t ignore this Queue Usage message is waiting here load balance Consume vs Get There exists ...

ScheduledThreadPool Implementation

When it comes to timeout job in Java, there exists many utility class to do so: java.util.Timer or Future . Java 5.0 introduced a more versitle utility class in concurrent pakage, ScheduledThreadPoolExecutor , to replace the original Timer class. It can execute a give task at given delay or rate and: This class is preferable to Timer when multiple worker threads are needed Today, we focus on how this executor is implemented. Static View As a subclass of ThreadPoolExecutor , it is composed of two main parts: Job queue Thread pool size max size keep live time thread factory rejection handler So, we will dive into those parts of implementations in the following section. Job Queue The job queue of ScheduledThreadPoolExecutor is a class called DelayedWorkQueue . This queue is based on priority queue which keeps the most urgent job on the top of heap. So every time, the thread will pick up the top of heap and run it if time is up without worry about other job...

A Request Sent to Spring(2): Example

In the last blog , we have learned the overview process how a Java server with Spring handle request. But in our daily usage, we are sometimes very confused about how to pass argument in, i.e. the conversion rule between the request string and our method parameters. So, today, we focus on common problem we met when use Spring MVC. Request Parameter @RequestParam is very common in our controller, which means this method parameter should interpreted from URL. From @RequestParam doc: If the method parameter type is Map and a request parameter name is specified, then the request parameter value is converted to a Map assuming an appropriate conversion strategy is available. If the method parameter is Map< String , String > or MultiValueMap< String , String > and a parameter name is not specified, then the map parameter is populated with all request parameter names and values. @GetMapping( "/foo" ) public String test(@Reqes...

A Request Sent to Spring(1)

This will be a serial of posts to understand how a HTTP request sent to Spring finally be handled by our business logical code and returned. Today, we will start from a HTTP request to invoke this code: // http://localhost:8080/testMapStr2?a=1&b=2 @GetMapping ( "/testMapStr" ) public String testMapStr (@RequestParam Map<String, String> m) { return m.toString(); } HTTP Request Format The essence of a protocol is the requested/conventional format of byte of data, which often starts with header, then data item. HTTP is not the special case. A post request is like following: POST /foo?bar = 2 Accept-Language: en other headers optinal body Server Handle So, when the server receive the http message from socket, it will interpret them as needed. In our case, we are using a Java server, so it will parse the URL (and check whether this URL meet the RFC of URL), extract method and headers, handle session etc. Then, It will construct a HttpRe...