{"id":13458749,"url":"https://github.com/alibaba/java-dns-cache-manipulator","last_synced_at":"2025-05-14T13:06:08.039Z","repository":{"id":29408663,"uuid":"32944108","full_name":"alibaba/java-dns-cache-manipulator","owner":"alibaba","description":"🌏 A tiny 0-dependency thread-safe Java™ lib for setting/viewing dns programmatically without touching host file, make unit/integration testing portable; and a tiny tool for setting/viewing dns of running JVM process.","archived":false,"fork":false,"pushed_at":"2025-04-14T09:36:00.000Z","size":1718,"stargazers_count":930,"open_issues_count":7,"forks_count":188,"subscribers_count":72,"default_branch":"main","last_synced_at":"2025-05-11T23:04:55.816Z","etag":null,"topics":["dns","dns-cache","dns-cache-manipulator","domain-name","hacking","hacking-code","integration-testing","java","jvm","testing","threadsafe","unit-testing"],"latest_commit_sha":null,"homepage":"https://github.com/alibaba/java-dns-cache-manipulator","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alibaba.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2015-03-26T18:11:56.000Z","updated_at":"2025-05-04T18:25:53.000Z","dependencies_parsed_at":"2023-09-28T11:14:32.720Z","dependency_job_id":"c1b10068-a76f-4f67-92eb-b2de2ce3dabc","html_url":"https://github.com/alibaba/java-dns-cache-manipulator","commit_stats":{"total_commits":448,"total_committers":7,"mean_commits":64.0,"dds":0.1428571428571429,"last_synced_commit":"619ab5a371bf23a2dc9957a97af46aa09f6a007f"},"previous_names":[],"tags_count":24,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alibaba%2Fjava-dns-cache-manipulator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alibaba%2Fjava-dns-cache-manipulator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alibaba%2Fjava-dns-cache-manipulator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alibaba%2Fjava-dns-cache-manipulator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alibaba","download_url":"https://codeload.github.com/alibaba/java-dns-cache-manipulator/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254149949,"owners_count":22022851,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dns","dns-cache","dns-cache-manipulator","domain-name","hacking","hacking-code","integration-testing","java","jvm","testing","threadsafe","unit-testing"],"created_at":"2024-07-31T09:00:56.636Z","updated_at":"2025-05-14T13:06:07.990Z","avatar_url":"https://github.com/alibaba.png","language":"Java","readme":"# \u003cdiv align=\"center\"\u003e\u003ca href=\"#dummy\"\u003e\u003cimg src=\"docs/logo-red.png\" alt=\"🌏 Java Dns Cache Manipulator(DCM)\"\u003e\u003c/a\u003e\u003c/div\u003e\n\n\u003cp align=\"center\"\u003e\n\u003ca href=\"https://github.com/alibaba/java-dns-cache-manipulator/actions/workflows/ci.yaml\"\u003e\u003cimg src=\"https://img.shields.io/github/actions/workflow/status/alibaba/java-dns-cache-manipulator/ci.yaml?branch=main\u0026logo=github\u0026logoColor=white\u0026label=fast ci\" alt=\"Github Workflow Build Status\"\u003e\u003c/a\u003e\n\u003ca href=\"https://github.com/alibaba/java-dns-cache-manipulator/actions/workflows/strong_ci.yaml\"\u003e\u003cimg src=\"https://img.shields.io/github/actions/workflow/status/alibaba/java-dns-cache-manipulator/strong_ci.yaml?branch=main\u0026logo=github\u0026logoColor=white\u0026label=strong ci\" alt=\"Github Workflow Build Status\"\u003e\u003c/a\u003e\n\u003ca href=\"https://codecov.io/gh/alibaba/java-dns-cache-manipulator/branch/main\"\u003e\u003cimg src=\"https://img.shields.io/codecov/c/github/alibaba/java-dns-cache-manipulator/main?logo=codecov\u0026logoColor=white\" alt=\"Coverage Status\"\u003e\u003c/a\u003e\n\u003ca href=\"https://codeclimate.com/github/alibaba/java-dns-cache-manipulator\"\u003e\u003cimg src=\"https://img.shields.io/codeclimate/maintainability/alibaba/java-dns-cache-manipulator?logo=code-climate\" alt=\"Maintainability\"\u003e\u003c/a\u003e\n\u003ca href=\"https://openjdk.java.net/\"\u003e\u003cimg src=\"https://img.shields.io/badge/Java-8+-339933?logo=openjdk\u0026logoColor=white\" alt=\"Java support\"\u003e\u003c/a\u003e\n\u003ca href=\"https://www.apache.org/licenses/LICENSE-2.0.html\"\u003e\u003cimg src=\"https://img.shields.io/github/license/alibaba/java-dns-cache-manipulator?color=4D7A97\u0026logo=apache\" alt=\"License\"\u003e\u003c/a\u003e\n\u003ca href=\"https://search.maven.org/artifact/com.alibaba/dns-cache-manipulator\"\u003e\u003cimg src=\"https://img.shields.io/maven-central/v/com.alibaba/dns-cache-manipulator?color=2d545e\u0026logo=apache-maven\u0026logoColor=white\" alt=\"Maven Central\"\u003e\u003c/a\u003e\n\u003ca href=\"https://github.com/alibaba/java-dns-cache-manipulator/releases\"\u003e\u003cimg src=\"https://img.shields.io/github/release/alibaba/java-dns-cache-manipulator.svg\" alt=\"GitHub release\"\u003e\u003c/a\u003e\n\u003ca href=\"https://alibaba.github.io/java-dns-cache-manipulator/apidocs/\"\u003e\u003cimg src=\"https://img.shields.io/github/release/alibaba/java-dns-cache-manipulator?label=javadoc\u0026color=339933\u0026logo=read-the-docs\u0026logoColor=white\" alt=\"Javadocs\"\u003e\u003c/a\u003e\n\u003ca href=\"https://github.com/alibaba/java-dns-cache-manipulator/stargazers\"\u003e\u003cimg src=\"https://img.shields.io/github/stars/alibaba/java-dns-cache-manipulator?style=flat\" alt=\"GitHub Stars\"\u003e\u003c/a\u003e\n\u003ca href=\"https://github.com/alibaba/java-dns-cache-manipulator/fork\"\u003e\u003cimg src=\"https://img.shields.io/github/forks/alibaba/java-dns-cache-manipulator?style=flat\" alt=\"GitHub Forks\"\u003e\u003c/a\u003e\n\u003ca href=\"https://github.com/alibaba/java-dns-cache-manipulator/issues\"\u003e\u003cimg src=\"https://img.shields.io/github/issues/alibaba/java-dns-cache-manipulator\" alt=\"GitHub issues\"\u003e\u003c/a\u003e\n\u003ca href=\"https://github.com/alibaba/java-dns-cache-manipulator/network/dependents\"\u003e\u003cimg src=\"https://badgen.net/github/dependents-repo/alibaba/java-dns-cache-manipulator?label=user%20repos\" alt=\"user repos\"\u003e\u003c/a\u003e\n\u003ca href=\"https://github.com/alibaba/java-dns-cache-manipulator/graphs/contributors\"\u003e\u003cimg src=\"https://img.shields.io/github/contributors/alibaba/java-dns-cache-manipulator\" alt=\"GitHub Contributors\"\u003e\u003c/a\u003e\n\u003ca href=\"https://github.com/alibaba/java-dns-cache-manipulator\"\u003e\u003cimg src=\"https://img.shields.io/github/repo-size/alibaba/java-dns-cache-manipulator\" alt=\"GitHub repo size\"\u003e\u003c/a\u003e\n\u003ca href=\"https://gitpod.io/#https://github.com/alibaba/java-dns-cache-manipulator\"\u003e\u003cimg src=\"https://img.shields.io/badge/Gitpod-ready to code-339933?label=gitpod\u0026logo=gitpod\u0026logoColor=white\" alt=\"gitpod: Ready to Code\"\u003e\u003c/a\u003e\n\u003ca href=\"https://github.com/alibaba/java-dns-cache-manipulator/releases/download/v1.8.3/dcm-1.8.3.tar.gz\"\u003e\u003cimg src=\"https://img.shields.io/github/downloads/alibaba/java-dns-cache-manipulator/v1.8.3/dcm-1.8.3.tar.gz.svg?logoColor=white\u0026logo=DocuSign\" alt=\"GitHub release download - dcm.tar.gz)\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n📖 English Documentation | 📖 [中文文档](docs/zh-CN/README.md)\n\n----------------------------------------\n\nJava Dns Cache Manipulator(`DCM`) contains 2 subprojects:\n\n- [**`DCM` Library**](library)  \n  A tiny 0-dependency thread-safe lib for setting/viewing dns programmatically without touching host file, make unit/integration test portable. Support `Java 8~21`, support `IPv6`.\n- [**`DCM` Tool**](tool)  \n  A tiny tool for setting/viewing dns of running JVM processes.\n\n\u003e [!NOTE]\n\u003e Start from `DCM 1.7+` upgrade to `Java 8` 🚀  \n\u003e If you need `Java 6` support, use version `1.6.x` \u003ca href=\"https://central.sonatype.com/artifact/com.alibaba/dns-cache-manipulator/versions\"\u003e\u003cimg src=\"https://img.shields.io/maven-central/v/com.alibaba/dns-cache-manipulator?versionPrefix=1.6.\u0026color=lightgrey\u0026logo=apache-maven\u0026logoColor=white\" alt=\"Maven Central\"\u003e\u003c/a\u003e\n\n----------------------------------------\n\n\u003c!-- START doctoc generated TOC please keep comment here to allow auto update --\u003e\n\u003c!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --\u003e\n\n- [Java Dns Cache Manipulator(`DCM`) Library](#java-dns-cache-manipulatordcm-library)\n  - [🔧 Features](#-features)\n  - [🎨 Requirement Scenario](#-requirement-scenario)\n  - [👥 User Guide](#-user-guide)\n    - [Set directly](#set-directly)\n    - [Batch configuration through the `dns-cache.properties` file](#batch-configuration-through-the-dns-cacheproperties-file)\n    - [View JVM DNS cache](#view-jvm-dns-cache)\n    - [Remove a DNS cache](#remove-a-dns-cache)\n    - [Clear JVM DNS cache](#clear-jvm-dns-cache)\n    - [Set/View the default DNS cache time of JVM](#setview-the-default-dns-cache-time-of-jvm)\n    - [Precautions for use](#precautions-for-use)\n      - [JVM settings for Java 16+](#jvm-settings-for-java-16)\n      - [Domain name case](#domain-name-case)\n      - [Domain resolution cache](#domain-resolution-cache)\n    - [More detailed functions](#more-detailed-functions)\n  - [🔌 Java API Docs](#-java-api-docs)\n  - [🍪 Dependency](#-dependency)\n  - [🗿 More Documentation](#-more-documentation)\n  - [📚 Related Resources](#-related-resources)\n- [Java Dns Cache Manipulator Tool](#java-dns-cache-manipulator-tool)\n  - [🔧 Features](#-features-1)\n  - [👥 User Guide](#-user-guide-1)\n    - [Download](#download)\n    - [Set/reset a DNS cache entry](#setreset-a-dns-cache-entry)\n    - [View DNS cache entry content](#view-dns-cache-entry-content)\n    - [Remove a DNS Cache](#remove-a-dns-cache)\n    - [Clear DNS Cache](#clear-dns-cache)\n    - [Set/View DNS cache time of `JVM`](#setview-dns-cache-time-of-jvm)\n    - [Load Cache Entries from File](#load-cache-entries-from-file)\n    - [Manipulate cache with a command line option at JVM startup](#manipulate-cache-with-a-command-line-option-at-jvm-startup)\n  - [📚 Related information](#-related-information)\n\n\u003c!-- END doctoc generated TOC please keep comment here to allow auto update --\u003e\n\n----------------------------------------\n\n# Java Dns Cache Manipulator(`DCM`) Library\n\n## 🔧 Features\n\n- Set/reset a DNS cache entry (won't lookup DNS afterwards)\n    - set a single `DNS record`\n    - or batch setting through a `Properties` file\n- View DNS cache entry content (positive dns cache and/or negative dns cache)\n- Remove a DNS cache entry (i.e. lookup DNS again)\n- Clear the DNS Cache (re-Lookup DNS for all domain names)\n- Set/View DNS cache time of `JVM` (positive dns cache and negative dns cache)\n\n## 🎨 Requirement Scenario\n\n1. The domain name is hard-coded in some libraries, and have to modify the `host file` binding to do the test(e.g. unit test, integration test). Turn out:\n    - Generally, developers do not have the permission to modify the `host file` on the continuous integration machine, which leads to the continuous integration fail.\n        - In fact, because of this, the demand for this library was born. 😣 🔫\n        - Unit testing requires each developer to do some host binding on the development machine, which increases configuration operations and is tedious and repetitive.\n2. Some functions require domain names instead of IPs as input parameters, such as HTTP gateways or web applications with domain name restrictions.\n    - In this case, you need a domain name to connect to the IP of the test machine; Or need use a test domain name that does not exist yet, but you do not want to or can not configure the DNS.\n3. In the performance test,\n    - want to skip DNS lookup through network (bypass the DNS resolution consumption), so that stress testing pays more attention to server response, and stress testing can fully reflect the performance of the core implementation code.\n    - DNS cache can be set dynamically instead of inflexible ways such as modifying host files and http links.\n    - A `JVM` process can have a set of domain name binding without affecting other JVM, be able to run stress testing with multi-scenario and multi-domain binding.\n4. When opening the `SecurityManager` in `Java` (such as a web application in the Web container `Tomcat`), `Java`'s DNS will not be expired by default. If the IP bound to the domain name changes, you can reset the DNS through this library.\n    - Set the running JVM DNS Cache through the `DCM` Tool.\n      application **need not** contain `DCM` Library dependency (i.e. `Jar`).\n    - Or call the method of `DCM` Library through the execution entry, such as remote call or [`jvm-ssh-groovy-shell`](https://github.com/palominolabs/jvm-ssh-groovy-shell).\n      The application **need** contain `DCM` Library dependency (ie `Jar`).\n\n## 👥 User Guide\n\nSet/View DNS through the class [`DnsCacheManipulator`](library/src/main/java/com/alibaba/dcm/DnsCacheManipulator.java).\n\n### Set directly\n\n```java\nDnsCacheManipulator.setDnsCache(\"hello.com\", \"192.168.1.1\");\n// support IPv6\nDnsCacheManipulator.setDnsCache(\"world.com\", \"1234:5678:0:0:0:0:0:200e\");\n\n// The above settings take effect globally, \n// and then all the domain name resolution logic in Java will be the IP set above.\n// Let's use a simple method to get the IP of the domain name to demonstrate:\n\nString ip = InetAddress.getByName(\"hello.com\").getHostAddress();\n// ip = \"192.168.1.1\"\nString ipv6 = InetAddress.getByName(\"world.com\").getHostAddress();\n// ipv6 = \"1234:5678:0:0:0:0:0:200e\"\n\n\n// set multiple IPs\nDnsCacheManipulator.setDnsCache(\"hello-world.com\", \"192.168.2.1\", \"192.168.2.2\");\n\nString ipHw = InetAddress.getByName(\"hello-world.com\").getHostAddress();\n// ipHw = 192.168.2.1, read the first IP\nInetAddress[] allIps = InetAddress.getAllByName(\"hello-world.com\");\n// Read the multiple IP setting\n\n\n// Set the expiration time, unit is milliseconds\nDnsCacheManipulator.setDnsCache(3600 * 1000, \"hello-hell.com\", \"192.168.1.1\", \"192.168.1.2\");\n```\n\n### Batch configuration through the `dns-cache.properties` file\n\nIn testing, it is expected that the domain name binding will be written in a configuration file.\n\nThe usage is as follows:\n\nprovide the file `dns-cache.properties` on ClassPath:\n\n```bash\n# Configuration format:\n# \u003chost\u003e = \u003cip\u003e\nhello-world.com=192.168.1.1\n# Support setting multiple IPs, separated by commas\nfoo.com=192.168.1.2,192.168.1.3\n# Support IPv6\nbar.com=1234:5678:0:0:0:0:0:200e\n```\n\nThen complete the batch setting with the following code:\n\n```java\nDnsCacheManipulator.loadDnsCacheConfig();\n```\n\n\u003e NOTE:\n\u003e\n\u003e The default configuration file name is `dns-cache.properties`.\n\u003e and the configuration file name used can be modified through the `-D` option `dcm.config.filename` of the `JVM`: `-Ddcm.config.filename=my-dns-cache.properties`.\n\n```java\n// or load the batch setting from the specified file name explicitly\nDnsCacheManipulator.loadDnsCacheConfig(\"my-dns-cache.properties\");\n```\n\nIn unit testing, it is often written in the `setUp` method of the test class, such as:\n\n```java\n@BeforeClass\npublic static void beforeClass() throws Exception {\n    DnsCacheManipulator.loadDnsCacheConfig();\n}\n```\n\n### View JVM DNS cache\n\n```java\n// Get a dns cache entry by host\nDnsCacheEntry entry = DnsCacheManipulator.getDnsCache(\"bing.com\");\n\n// get whole dns cache info DnsCache including cache and negative cache.\nDnsCache dnsCache = DnsCacheManipulator.getWholeDnsCache();\n\n// get positive dns cache entries\n//   same as DnsCacheManipulator.getWholeDnsCache().getCache()\nList\u003cDnsCacheEntry\u003e positiveEntries = DnsCacheManipulator.getWholeDnsCache();\n\n// get dns negative cache entries\n//   same as DnsCacheManipulator.getWholeDnsCache().getNegativeCache()\nList\u003cDnsCacheEntry\u003e positiveEntries = DnsCacheManipulator.getWholeDnsCache();\n```\n\n### Remove a DNS cache\n\naka. relookup DNS later.\n\n```java\nDnsCacheManipulator.removeDnsCache(\"bing.com\");\n```\n\n### Clear JVM DNS cache\n\n```java\nDnsCacheManipulator.clearDnsCache();\n```\n\n### Set/View the default DNS cache time of JVM\n\n```java\n// View the cache time, in seconds. -1 means cache forever, 0 means never cache\nint cachePolicy = DnsCacheManipulator.getDnsCachePolicy();\n// Set the cache time\nDnsCacheManipulator.setDnsCachePolicy(2);\n\n// View the cache time of missed entries(negative entries)\nDnsCacheManipulator.getDnsNegativeCachePolicy();\n// Set the cache time of missed entries\nDnsCacheManipulator.setDnsNegativeCachePolicy(0);\n```\n\n### Precautions for use\n\n#### JVM settings for Java 16+\n\nWith the release of Java 16 the access control of the new Jigsaw module system is starting to be enforced by the JVM. If you use `DCM` under Java 16+, add below Java options:\n\n```sh\n--add-opens java.base/java.net=ALL-UNNAMED\n--add-opens java.base/sun.net=ALL-UNNAMED\n```\n\nIf you use `maven`(e.g. running test), add below config:\n\n```xml\n\u003cprofiles\u003e\n    \u003cprofile\u003e\n        \u003cid\u003eadd-java-open-options-for-jdk16+\u003c/id\u003e\n        \u003cactivation\u003e\n            \u003cjdk\u003e[16,)\u003c/jdk\u003e\n        \u003c/activation\u003e\n        \u003cproperties\u003e\n            \u003cargLine\u003e\n                --add-opens java.base/java.net=ALL-UNNAMED\n                --add-opens java.base/sun.net=ALL-UNNAMED\n            \u003c/argLine\u003e\n        \u003c/properties\u003e\n    \u003c/profile\u003e\n\u003c/profiles\u003e\n```\n\n#### Domain name case\n\nThe domain name is not case-sensitive.\n\nThe domain name may be converted to lower case uniformly before entering the `JVM` DNS Cache.\n\nOne of the causes is that the case of the domain name in the DNS query result will be different from the case of the entered domain name, if the entered domain name has uppercase letters.\n\n#### Domain resolution cache\n\n- For the logic that has been resolved and saved the IP, setting the JVM DNS cache will not take effect! This can be resolved by re-creating the connection or the client.\n\nFor `HttpClient`:\n\n```java\nHttpClient client = new HttpClient();\nGetMethod m1 = new GetMethod(\"https://bing.com\");\nclient.executeMethod(m1);\nString content = m1.getResponseBodyAsString();\n\n// Set up DNS and bind to your own machine\nDnsCacheManipulator.setDnsCache(\"bing.com\", \"192.168.1.1\");\n\n// Re-execute m1, still the old result\nclient.executeMethod(m1);\nString content = m1.getResponseBodyAsString();\n\n// Re-create GetMethod to get the results on your own machine\nGetMethod m2 = new GetMethod(\"https://bing.com\");\nclient.executeMethod(m2);\ncontent = m2.getResponseBodyAsString();\n```\n\n### More detailed functions\n\nSee the documentation for the class [`DnsCacheManipulator`](library/src/main/java/com/alibaba/dcm/DnsCacheManipulator.java).\n\n## 🔌 Java API Docs\n\nJava API document: \u003chttp://alibaba.github.io/java-dns-cache-manipulator/apidocs\u003e\n\n## 🍪 Dependency\n\n`Maven` example:\n\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003ecom.alibaba\u003c/groupId\u003e\n    \u003cartifactId\u003edns-cache-manipulator\u003c/artifactId\u003e\n    \u003cversion\u003e1.8.3\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\nYou can view the latest version at [search.maven.org](https://search.maven.org/artifact/com.alibaba/dns-cache-manipulator).\n\n## 🗿 More Documentation\n\n- [🎓 Developer Guide](docs/developer-guide.md)\n\n## 📚 Related Resources\n\n- Article [Source code analysis of java dns parsing cache / `java dns`解析缓存之源码解析](http://rongmayisheng.com/?p=1006) is very complete and have source code analysis, give noteworthy conclusions:\n    - Open the SecurityManager in Java, the DNS cache will not be invalidated.\n    - Otherwise, the accessible DNS resolution will be cached for 30 seconds by default, and the inaccessible DNS resolution will be cached for 10 seconds by default.\n- [Regarding the jvm dns cache (domain name cache time) / 关于`jvm dns cache`(域名缓存时间)](https://nigelzeng.iteye.com/blog/1704052), the conclusion of \"what strategy is used to return IP for multiple A records\" is given:\n    - During the validity period of the cache, the obtained IP is always the first A records in the cache, and there is no such strategy as round-robin.\n    - After the cache is invalidated, perform DNS resolution again. Because the order of the A records returned by the domain name resolution will change (visible in the dig google.com test), the order of the data in the cache has also changed, and the obtained IP will also change.\n- [Modify the content of DNS cache in JDK 1.6 through JAVA reflection / 通过`JAVA`反射修改`JDK 1.6`当中`DNS`缓存内容](https://tuicool.com/articles/auYzui), give the scene of setting DNS cache in performance test.\n- [The dns cache problem of java InetAddress / java InetAddress 的 dns cache 问题](http://blogjava.net/jjwwhmm/archive/2008/07/09/213685.html) indicates that `HttpClient` needs to recreate the `GetMethod`/`PostMethod` object to make the DNS setting take effect.\n\n# Java Dns Cache Manipulator Tool\n\n## 🔧 Features\n\n- Set/reset a DNS cache entry\n- View DNS cache entry content\n- Remove a DNS Cache\n- Clear DNS Cache\n- Set/View DNS cache time of `JVM`\n\n## 👥 User Guide\n\n### Download\n\n[![GitHub release download - dcm.tar.gz)](https://img.shields.io/github/downloads/alibaba/java-dns-cache-manipulator/v1.8.3/dcm-1.8.3.tar.gz.svg?logoColor=white\u0026logo=DocuSign)](https://github.com/alibaba/java-dns-cache-manipulator/releases/download/v1.8.3/dcm-1.8.3.tar.gz) download the file `dcm-x.y.z.tar.gz`.\n\nAfter decompression, run `dcm` in the `bin` directory.\n\n```bash\n$ dcm -h\nusage: Options\n -h,--help             show help\n -p,--pid \u003carg\u003e        java process id to attach\n```\n\n### Set/reset a DNS cache entry\n\n```bash\n# For the Java process whose process ID is 12345\n# set the domain name foo.com IP to 1.1.1.1\n$ dcm -p 12345 set foo.com 1.1.1.1\n# For the Java process whose process ID is 12345\n# set the domain name bar.com IP to 2.2.2.2 3.3.3.3 (multiple IPs are possible)\n$ dcm -p 12345 set bar.com 2.2.2.2 3.3.3.3\n```\n\n### View DNS cache entry content\n\nView single entry:\n\n```bash\n# For the Java process whose process ID is 12345,\n# obtain the DNS entry information of the domain name bing.com\n$ dcm -p 12345 get bing.com\nbing.com 220.181.57.217,180.149.132.47,123.125.114.144 2015-06-05T18:56:09.635+0800\n# The output format:\n#   \"the domain name\" \"IP list (there may be multiple IPs)\" \"expiration time\"\n```\n\nView all DNS cache:\n\n```bash\n$ dcm -p 12345 list\nDns cache:\n    bar.com 2.2.2.2,3.3.3.3 292278994-08-17T15:12:55.807+0800\n    bing.com 220.181.57.217,180.149.132.47,123.125.114.144 2015-06-05T19:00:30.514+0800\n    foo.com 1.1.1.1 292278994-08-17T15:12:55.807+0800\nDns negative cache:\n# Output entries containing Cache and Negative Cache.\n# The entry is indented 4 spaces.\n# In the above example, Negative Cache is empty.\n```\n\n### Remove a DNS Cache\n\n```bash\n# Remove a DNS\n$ dcm -p 12345 rm bing.com\n```\n\n### Clear DNS Cache\n\n```bash\n# Clear all DNS Cache\n$ dcm -p 12345 clear\n```\n\n### Set/View DNS cache time of `JVM`\n\n```bash\n# View the cache time, in seconds.\n# -1 means cache forever, 0 means no cache\n$ dcm -p 12345 getPolicy\n30\n# Set cache time\n$ dcm --pid 12345 setPolicy 5\n# View the cache time of missed entries, in seconds.\n# -1 means cache forever, 0 means no cache\n$ dcm -p 12345 getNegativePolicy\n10\n# Set the cache time of missed entries\n$ dcm -p 12345 setNegativePolicy 0\n```\n\n### Load Cache Entries from File\n\n```bash\n$ dcm -p 12345 load /foo/bar/my-cache.properties\n```\n\n### Manipulate cache with a command line option at JVM startup\n\nTo manipulate dns entries at jvm startup, add the following jvm-options:\n\n```bash\n# Set individual entry\njava -javaagent:\u003cpath to jar\u003e=\"set foo.com 1.1.1.1\" --add-opens java.base/java.net=ALL-UNNAMED --add-opens java.base/sun.net=ALL-UNNAMED ...\n# Load entries from file\njava -javaagent:\u003cpath to jar\u003e=\"load my-cache.properties\" --add-opens java.base/java.net=ALL-UNNAMED --add-opens java.base/sun.net=ALL-UNNAMED ...\n```\n\n## 📚 Related information\n\n* [Java Agent Specification](http://docs.oracle.com/javase/7/docs/api/java/lang/instrument/package-summary.html)\n* [New Features of Java SE 6: New Features of Instrumentation](http://www.ibm.com/developerworks/cn/java/j-lo-jse61/)\n* [Creation, dynamic loading and instrumentation with javaagents](http://dhruba.name/2010/02/07/creation-dynamic-loading-and-instrumentation-with-javaagents/)\n* [Analysis of JavaAgent loading mechanism](http://nijiaben.iteye.com/blog/1847212)\n","funding_links":[],"categories":["Java","网络编程"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falibaba%2Fjava-dns-cache-manipulator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falibaba%2Fjava-dns-cache-manipulator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falibaba%2Fjava-dns-cache-manipulator/lists"}