Skip to content

Instantly share code, notes, and snippets.

What is a web service?

Web Service is a software system designed to support interoperable machine to machine interaction over a network

Three key features for a web service:

  • Should be designed for machine to machine interaction
  • Should not have platform dependent
  • Should allow communication over network
Deadlock is a permanent blocking of a set of threads that are competing for a set of resources.
Generally speaking, a deadlock requires the following four conditions:
1, a limited number of resources
2, Once a thread is blocked, all the resources it has required will be kept by it
3, One thread could not steal resource from another before it is been released
4, there is a circular wairesource requirement loop
The most common error causing deadlock is self deadlock or recursive deadlock
To beak a deadlock:

A Stream is a sequence of data elements supporting sequential and parallel aggregate operations.

A Stream consists of three parts: a source, an set of intermedia operations and one terminal operation

How to create a stream

  • Stream.of(T... values): return a stream with values
  • Arrays.stream(T[] array): return a stream of the array
  • Files.lines(Paths.get(String)): return a stream of String whose item is a line of a file

null VS Optional

The use of null references in Java causes both theoretical and practical problems:

  • It’s a source of error. NullPointerException is by far the most common exception in Java.
  • It bloats your code. It worsens readability by making it necessary to fill your code with often deeply nested null checks.
  • It’s meaningless. It doesn’t have any semantic meaning, and in particular it represents the wrong way to model the absence of a value in a statically typed language.
  • It breaks Java philosophy. Java always hides pointers from developers except in one case: the null pointer.
  • It creates a hole in the type system. null carries no type or other information, meaning it can be assigned to any reference type. This is a problem because, when it’s propagated to another part of the system, you have no idea what that null was initially supposed to be.

软件的核心 = 数据 处理

1. type(数据类型)

限制了数据存储和表示的格式,一种数据类型包括:

  • 一组可以表示的值
  • 一组可以应用的运算符

By default, Spring Boot provides an /error mapping that handles all errors in a sensible way, and it is registered as a “global” error page in the servlet container. To add a customized exception handler you could:

First, defind a Base Exception for this application, note that:

  • The base exception extends RuntimeException
  • There is a exceptionType and Date field in the Base Exception
  • Exception message is held in the Runtime exception
  • When a exception is thrown, its getMessage() will be called to print out details, that is why we override getMessage()

First, you need create a LocaleResolver bean to set default locale. The following code means that your default locale is US

    @Bean
    public LocaleResolver getLocalResolver(){

        SessionLocaleResolver localeResolver = new SessionLocaleResolver();
        localeResolver.setDefaultLocale(Locale.US);
        return localeResolver;
    }

docker

- Docker enables you to separate your applications from your infrastructure so you can deliver software quickly
  • ps: see a list of running containers
  • version: get version of docker
  • info: get a lot of info about docker engine
  • build: build a docker image from a docker file

Assume that you have a docker container whose name is 'c1', which is based on some public image and you did a lot of changes after the first run.

  • You can make the container as a image with current state by docker commit c1 new_image:v1
  • You can save the image as a tar file with following command docker save -o image.tar new_image:v1
  • Then you can send the file to others in your team, who can load the image by docker load -i image.tar
  • They can run a container with this image 'docker run --name c2 -it new_image:v1'

Behaviour Parameterization

  • Behaviour Parameterization gives us the ability to tell a method to take multiple behaviours (or strategies) as parameters and use them internally to accomplish different behaviours
  • to achieve this, you should abstract the behaviours as an interface with only one abstract method, let the function which contains the behaviour take an interface as a parameter, for example:
    //data type classification