Home >Java >javaTutorial >Java WeChat development (wechat4j) - access_token central control server implementation
Access_token is a credential during the interaction with the WeChat server. Every time the client server actively communicates with the WeChat server, it needs to bring the access_token to confirm its identity. Wechat4j internally encapsulates the operation of access_token, including obtaining access_token and the implementation of the access_token central control server.
Access_token acquisition
To obtain access_token, you can use the following method
String accessToken = TokenProxy.accessToken();
access_token The central control server
access_token has a daily limit, so the client server cannot request a new access_token every time. After each request, the access_token has an expiration time. Therefore, the WeChat platform recommends that you use a central control server to refresh the token regularly. After obtaining it, you can save it without having to request the token again, because the number of access_token requests is limited. There are only two benefits to doing this:
Ensure that the access_token will not exceed the access limit every day, ensuring the normal service.
Improving the performance of the service, there is no need to send an access_token acquisition request before each business request is sent.
wechat4j obtains access_token through the proxy TokenProxy of access_token, and this proxy encapsulates the central control server of access_token. There are two ways to save access_token, one is to save in memory and the other is to save persistently (database or file). The central control server in memory storage mode is implemented by wehcat4j, and the customer does not need to do any operations. If customers want to use persistent storage, they need to define the central control server themselves. Therefore, there are two types of central control servers as follows:
access_tokenDefault central control server
access_tokenCustomized central control server
Default central control server
wechat4j The default central control server is in memory mode, which means that the access_token is stored in memory until it expires and then requests a new one to replace it. The class corresponding to the default central control server is AccessTokenMemServer.
Advantages: The central control server in this mode is highly efficient and easy to use, and customers do not need to worry about it.
Disadvantages: Multi-server clusters cannot be supported. If there are multiple servers, this method will not be supported.
Customized central control server
If you need to save the access_token in a database or file, then you need to use a custom central control server. The customized server needs to be completed by the customer. It must inherit the abstract class CustomerServer and complete the saving and query methods. At the same time, the customized server is driven by a timer and needs to configure a listener.
Detailed steps for configuring a custom access_token server:
To create your own server class, you need to inherit the CustomerServer class of wechat4j and implement the save and find methods. The former is to save the access_token to the database, and the latter is The method of querying access_token from the database can be a sql operation or the like.
public class CustomerAccessTokenServer extends CustomerServer{/* (non-Javadoc) * @see org.sword.wechat4j.token.DbAccessTokenServer#find() */@Overridepublic String find() { String accessToken = null; //执行数据库操作// String sql = "select cfgValue from cfg where cfg.cfgKey = 'access_token'";// accessToken = DBUtil.query(sql); return accessToken; }/* (non-Javadoc) * @see org.sword.wechat4j.token.DbAccessTokenServer#save() */@Overridepublic boolean save(Token accessToken) { //如果没有需要插入,如果有的就更新,假设已经有了数据库配置项// String sql = "update cfg set cfg.cfgValue=" + accessToken.getToken() + // " where cfg.cfgKey= 'access_token'";// DBUtil.execute(sql); return true; } }
Configure the wechat.accessToken.server.class item in the wechat4j.properties configuration file, and match the path of the newly created server class
wechat.accessToken.server.class=com.sample.wechat.CustomerAccessTokenServer
Configure wechat4j's access_token monitoring in web.xml device. The code is as follows:
<listener> <listener-class>org.sword.wechat4j.token.TokenListener</listener-class></listener>
You can configure your own database access_token server through the above three steps. In this way, the central control server refreshes the access_token regularly and then saves it to the database. When the access_token is used in the business logic, the data is obtained from the database through a proxy.
The scheduled refresh task will run 200 seconds in advance to prevent the new access_token from expiring after the expiration of the old access_token.
For more java WeChat development (wechat4j) - access_token central control server implementation related articles, please pay attention to the PHP Chinese website!