Monday, 26 June 2023
How to Configure DataBase TimeZone
Friday, 28 April 2023
Maven Review
Project Coordinate
Coordinate of the maven project is used to uniquely identify a maven project, and locate it. compulsorily, it requires the followings: group id: com.xxx; artefact id: project name;version: version control.
project name: the name of the project.
Parent POM
Parent pom must be packaged as a POM.
Effective POM
Maven project is set up with default behaviors. It because Maven POM automatically extends from a super POM; the following command may show the effective pom file.mvn help:effective-pom
Maven Project Structure
Maven project has a fixed file structure to follow.The POM file is located at a project root.
src folder:
The source is located at the src/main
Java code is located at the src/main/java
Resources are located at src/main/resources
The test is located at the src/test.
Java test code is located at the src/test/java
Test resources are located at src/test/resources
target folder:
used to take all compiled results.
Build Lifecycle
- validate: is all the necessary information for this project available?
- compile: compile the source code of the project, and put them in the target folder.
- test: test the compiled source code using a suitable unit test framework.
- package: take the compiled code and package it in its distribution format, fx: jar.
- verify: run integration tests to ensure quality criteria are met.
- install: install the package into the local maven repository for other project's dependency.
- deploy: copying the final package to the remote repository, for sharing with other developers.
Plugins
mvn maven-plugin-name:goal
- Build plugins: defined under the <build>
- Report plugins: defined under the <reporting>
Archetype
The archetype is a Maven project template. A developer may create a project base on an archetype.mvn archetype: generate
maven-plugin: archetype; and goal: generate
Using maven archetype plugin to generate a web app project skeleton from a command line.
mvn archetype:generate -DgroupId=com.ynz -DartifactId=sampleWeb -DarchetypeArtifactId=maven-archetype-webapp -DarchetypeVersion=1.4 -DinteractiveMode=false
Package
Dependency Scope
- Compile a default dependency scope. The dependent jar is available in all the stages.
- Test: The dependent jar is available during the test. It is designed to reduce the size of the artifact.
- Import: indicates this dependency(like BOM) will be replaced by another POM.
Force Repository Update
- mvn package
- mvn install
- mvn dependency:resolve
Corrupted Dependencies
- mvn package -U
- mvn install -U
- mvn dependency:resolve -U
Analyse Maven Dependencies
- mvn dependency:analyze
- mvn dependency:tree
- Dependency convergence issues: When different versions of the same dependency are used in the project, which can lead to runtime errors.
- Unused dependencies: When a dependency is declared in the project but is not actually used by any code.
- Missing dependencies: When a dependency is required by the project but is not declared in the pom.xml file.
- Dependency conflicts: When multiple dependencies declare the same artifact with different versions.
Mediation Principle
Skipping Tests
To skip running the tests for a particular project, set the skipTests property to true.
- <project>
- [...]
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-surefire-plugin</artifactId>
- <version>3.0.0</version>
- <configuration>
- <skipTests>true</skipTests>
- </configuration>
- </plugin>
- </plugins>
- </build>
- [...]
- </project>
You can also skip the tests via the command line by executing the following command:
- mvn install -DskipTests
If you absolutely must, you can also use the maven.test.skip property to skip compiling the tests. maven.test.skip is honored by Surefire, Failsafe and the Compiler Plugin.
- mvn install -Dmaven.test.skip=true
Reference:
Wednesday, 26 April 2023
Linux
Linux principles:
- everything in Linux is a file
- small a single purpose programs
- chain programs together to perform a complex purpose
- avoid user interface
- configuration in a text file
Terms:
Bash is a sort of Shell, other shells, fx: Fish, Zsh, Ksh
A Terminal, also called a shell, is a GUI to interact with the kernel.
Linux folder
/(forward slash): Root
/home/username: each user has a personal folder
/bin: command binaries.
/etc: local system configuration files
/lib: shared libs shared for system boot
/opt: the third-party tools
/var: variable files, fx: log files
Linux commands
ls -a: list all files including hidden ones.
ls -l: list files in a long format.
ls -l *.txt
cd: change the current directory
.. indicates the parent folder
mkdir: make a new folder
mkdir folder1 folder2: creating several folders
mkdir -p folder/nested-folder: creating m-nested folders
rmdir: remove a folder
rm: remove one file or multiple files
rm blue red yellow
rm -v blue red yellow
-v: verbose. -r: recursive -i: interactive -f: force
rm -rf folder-name
touch: if the file is not existed, create one, else update to a new timestamp.
touch new-file-name
open: open a file or directory just like double-clicked file icon.
open. open the current folder in a graphic presentation
open file-name: open the file in OS default text editor
> : redirect output console
date > newFile.text: output current date to designated file, and overwrite the file entirely.
>>: redirect out the console and appending
date>> newFile.text: out current date to designated file, and appending.
cp: copy file or folder
less: a UI showing the entire file, scrolling down and up.
less file-name
WC: word count
wc -l file-name: shows line number in a file
wc -w file-name: shows how many words are in a file
piping |: taking one command output as input of another command.
Env var
expending dollar sign or tilde
dollar sign path
echo * all path names
echo *.txt
? matching each single character
cry0l1t3@htb[/htb]$ ls -l /etc/passwd
- rwx rw- r-- 1 root root 1641 May 4 23:42 /etc/passwd
- --- --- --- | | | | |__________|
| | | | | | | | |_ Date
| | | | | | | |__________ File Size
| | | | | | |_______________ Group
| | | | | |____________________ User
| | | | |_______________________ Number of hard links
| | | |_ Permission of others (read)
| | |_____ Permissions of the group (read, write)
| |_________ Permissions of the owner (read, write, execute)
|____________ File type (- = File, d = Directory, l = Link, ... )chmod 777 file-name : change file read-write-executable
find . -name fileName: find the current folder by name
which: shows the path to a file
which file-name: locate a file
ps: shows all processes
ps -ef: extended format
ps -ef | grep bash
kill PID: kill by its process ID
top: show statistics of system usage
wget URI: download resources
su: switch user to log in as someone else
exit: log out a user account
sudo: super user do, run a command as a root user
sudo apt update: update the APT package index in line with the repository.
sudo apt upgrade: update the installed packages to the latest version.
sudo apt upgrade specific-package: update an installed package to the newest version.
sudo apt install package-name: install package
source file-name [argument]: shell built-in command that can execute a file containing a list of shell commands.
export: built-in shell command to mark env variable to export the child processes(change visibilities).
sh xxx.sh: execute a script file
Vi/Vim Editor
Network
Check Remote Ports are Reachable Using ‘nc’ Command
-v – enables verbose mode.
The next command will check if ports 80, 22 and 21 are open on the remote host 192.168.5.10 (we can use the hostname as well):
nc -zv 192.168.56.10 80 22 21
Friday, 14 April 2023
Concurrent Programming Review
Process and Thread
The process is self-contained. It doesn't share the resource with others. A thread is a minimal execution unit, living inside a process. One process may consist of many threads. Threads may share a common resource with each other.
Volatile: Solving the visibility issue between threads.
Each Thread has its own local cache, so the variable defined in a thread won't be visible by another thread. When using Volatile keyword to modify a variable, the OS will push the variable into the shared cache(among the threads), thus it becomes visible to all other threads.
Concurrent and Parallel computing
Mutex
Data race:
Simultaneously multiple threads access a common memory location. The data stored there may be not consistent and being polluted. Ideally, all steps should be carried out in a single uninterruptible operation, so as to prevent data from pollution.Lock:
Two types of locks, i.e. extrinsic or intrinsic lock.An extrinsic lock is explicitly declared from Lock interface and its implementations. A critical section is protected by an explicit lock and afterwards, the lock is unlocked.
Synchronization:
Atomic variables:
atomicity guarantees by using Low-level compare-and-set(CAS) instruction.
Try Lock:
When a thread wants to acquire the lock, but not available, it may go away to carry out some other tasks, rather than being blocked and waiting there. This is the purpose to have 'try lock' method.
Read-write lock:
Note: read-write lock may use more resource under the hood.
Liveness
Deadlock:
lock priority: multiple threads acquire locks in a fixed order.
lock timeout: if cannot acquire, then quit.
Abandoned lock:
How to prevent a lock becoming abandoned?
using try-catch block to surround the critical section, and in the 'finally' block unlock the lock.
Starvation:
Starvation happens when a thread is perpetually denied resources due to competition with others. A thread may in starvation because it has a low priority. Or it simply because there are too many concurrent threads.Livelock:
Multiple threads are designed to respond to each other so as to solve the conflict.To solve the livelock, making sure only one thread takes action. It may take use of random selection.
Monday, 10 April 2023
Explicit Lock vs Implicit Lock
Explicit locks, such as those provided by the java.util.concurrent.locks package (e.g., ReentrantLock, ReadWriteLock), offer several advantages over using intrinsic locks provided by synchronized blocks or methods. Some of the key benefits of using explicit locks are:
Flexibility: Explicit locks offer more flexibility in terms of lock acquisition and release. You can lock and unlock the lock object at different scopes and in separate blocks of code, which is not possible with
synchronizedblocks or methods.Fairness:
ReentrantLockallows you to specify fairness when creating the lock object. When a lock is fair, it grants access to the longest-waiting thread. Intrinsic locks provided bysynchronizedblocks or methods do not have a fairness option and may lead to thread starvation in some scenarios.
javaLock lock = new ReentrantLock(true); // Create a fair lock
- Try to acquire lock: Explicit locks provide the
tryLock()method, which allows a thread to attempt to acquire the lock without waiting indefinitely. This can help avoid deadlocks and improve performance in some cases.
javaif (lock.tryLock()) {
try {
// Perform the operation
} finally {
lock.unlock();
}
} else {
// Handle the lock not being acquired
}
- Interruptible lock acquisition: With explicit locks, you can use the
lockInterruptibly()method, which allows a thread to be interrupted while waiting to acquire the lock. Intrinsic locks do not support this functionality.
javatry {
lock.lockInterruptibly();
try {
// Perform the operation
} finally {
lock.unlock();
}
} catch (InterruptedException e) {
// Handle the interruption
}
- Separate read and write locks: The
ReadWriteLockinterface provides separate locks for read and write operations, allowing multiple threads to read shared data simultaneously while still providing exclusive access for write operations. This can lead to better performance in scenarios with more reads than writes.
javaReadWriteLock rwLock = new ReentrantReadWriteLock();
Lock readLock = rwLock.readLock();
Lock writeLock = rwLock.writeLock();
While explicit locks offer several advantages, they also require more careful handling and can be more error-prone due to the need for manual lock management. When choosing between explicit locks and intrinsic locks, consider the specific use case, the complexity of the code, and the required functionality.
Wednesday, 29 March 2023
URL Special Signs
For example, a URL that includes a query string parameter for a search term might look like this:
https://www.example.com/search?term=best+restaurantsIn this URL, the plus sign is used to represent the space between "best" and "restaurants" in the search term "best restaurants". When the web server receives this request, it would interpret the plus sign as a space and perform the search accordingly.
If you need to include a plus sign (+) as a literal character in a URL, you can use URL encoding to represent it as %2B.
URL encoding is a process in which special characters are replaced with their corresponding hexadecimal ASCII codes. This allows them to be transmitted over the internet without interfering with the meaning of the URL.
So if you want to include a plus sign in a URL, you can replace it with %2B. For example, the following URL includes a literal plus sign in the parameter value:
https://www.example.com/search?query=2+2=4%2B2
In this case, the plus sign between "4" and "2" is represented by %2B, which is the URL-encoded value of the plus sign character. When the web server receives this request, it would interpret the %2B code as a plus sign and perform the search accordingly.
Can Jackson Deserialize Java Time ZonedDateTime
Yes, but must include JSR310. Thus ZonedDateTime can be deserialized directly from JSON response to POJO field. <dependency> <g...
-
The strictfp keyword is used to force the precision of floating point calculations (float or double) in Java conform to IEEE’s 754 standard...
-
In construction. Spring test annotations: @SpringBooTest @DataJpaTest @TestPropertySource @ActiveProfiles @Sql @SpringBootTest It is used f...
-
Testing Controllers @Runwith(SrpingRunner.class) Unit test @Runwith(SrpingRunner.class) @WebMvcTest(SpecificController.class) Integration te...