Integration with spring
What jar packages I need.
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId> spring-data-redis</artifactid>
</dependency>
<dependency>
<groupId> redis.clients</groupid>
<artifactId>jedis</artifactId>
</dependency>
How to configure the spring configuration file.
<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "/http Www.springframework.org/schema/beans "xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "xmlns:p="/HTTP/ www.springframework.org/schema/p "xsi:schemalocation=" Http://www.springframework.org/schema/beans/http Www.springframework.org/schema/beans/spring-beans.xsd "> <!--Configure IP address and port number, connect to Redis server--<bean id=" Jedisc Onnectionfactory "class=" Org.springframework.data.redis.connection.jedis.JedisConnectionFactory "P:hostname=" 127.0.0.1 "p:port=" 6379 "p:usepool=" true "/> <!--configuration redistemplate--> <bean id=" redistemplate "class=" org . Springframework.data.redis.core.RedisTemplate "p:connectionfactory-ref=" Jedisconnectionfactory "/> <!-- Configure stringredistemplate--> <bean id= "stringredistemplate" class= "
Org.springframework.data.redis.core.StringRedisTemplate "p:connectionfactory-ref=" Jedisconnectionfactory "/> </beans>
General, only need to configure redistemplate or stringredistemplate one on the line, commonly used for stringredistemplate.
The difference between redistemplate and stringredistemplate.
Stringredistemplate:
public class Stringredistemplate extends Redistemplate<string, string> {public
stringredistemplate () {
Stringredisserializer Stringserializer = new Stringredisserializer ();
This.setkeyserializer (Stringserializer);
This.setvalueserializer (Stringserializer);
This.sethashkeyserializer (Stringserializer);
This.sethashvalueserializer (Stringserializer);
}
Public stringredistemplate (Redisconnectionfactory connectionfactory) {this
();
This.setconnectionfactory (connectionfactory);
This.afterpropertiesset ();
}
Protected redisconnection preprocessconnection (redisconnection connection, Boolean existingconnection) {
return New Defaultstringredisconnection (connection);
}
}
As can be seen from the code above, Stringredistemplate inherits the redistemplate of the key value type string and uses Stringredisserializer as the serialization tool. So stringredistemplate can use the method, Redistemplate can use, the following example will only show the Stringredistemplate method.
Stringredisserializer:
public class Stringredisserializer implements redisserializer<string> {
private final Charset Charset;
Public Stringredisserializer () {This
(Charset.forname ("UTF8"));
}
Public Stringredisserializer (Charset Charset) {
assert.notnull (Charset);
This.charset = CharSet;
}
public string deserialize (byte[] bytes) {
return bytes = = Null?null:new String (bytes, this.charset);
}
Public byte[] Serialize (string string) {
return String = = Null?null:string.getbytes (this.charset);}
}
As you can see, Stringredisserializer uses the UTF8 character set to process strings. using the API to manipulate basic Redis basic data Types
Spring provides which interfaces operate Redis basic data.
First Group
Valueoperations: String type operation
listoperations: List type operation
setoperations: Collection type operation
zsetoperations: Ordered collection type Operation
Hashoperations: Hash operation
Second group
Boundvalueoperations: String type operation
boundlistoperations: List type action
boundsetoperations: Collection type action
Boundzsetoperations: Ordered collection type operation
boundhashoperations: hash operation
How to get the implementation of the interface.
First Group
valueoperations<string, string> valueoperations = Stringredistemplate.opsforvalue ();
listoperations<string, string> listoperations = Stringredistemplate.opsforlist ();
setoperations<string, string> setoperations = Stringredistemplate.opsforset ();
zsetoperations<string, string> zsetoperations = Stringredistemplate.opsforzset ();
Hashoperations<string, Object, object> hashoperations = Stringredistemplate.opsforhash ();
Second group
boundvalueoperations<string, string> valueoperations = Stringredistemplate.boundvalueops ("key");
boundlistoperations<string, string> listoperations = Stringredistemplate.boundlistops ("key");
boundsetoperations<string, string> setoperations = Stringredistemplate.boundsetops ("key");
boundzsetoperations<string, string> zsetoperations = Stringredistemplate.boundzsetops ("key");
Boundhashoperations<string, Object, object> hashoperations = Stringredistemplate.boundhashops ("key");
From the above two sets of implementations can be found that the second set of APIs just above the first set of APIs on the key value of the binding on the acquisition interface, the move is convenient for each operation of the basic data type without repeated to fill in the key value, only need to manipulate the specific value of the line.
Specific data operation methods, such as Valueoperations get and set,listoperations push and pop, etc. can be referred to: Redis learning Note (2)-redis data type. Their method signature is the same as the signature of the client when REDIS-CLI operations Redis. using the API to manipulate Message Queuing
Before using the API, you need to understand
Redis Learning Note (3)-redis transaction, expiration time, queue
How to send a message queue.
Redistemplate template = Template.convertandsend ("channel", "message");
The first parameter is the channel of the message being sent, and the second parameter is the message itself.
How to accept messages in a queue.
Public interface Messagedelegate {
void Handlemessage (String message);
void Handlemessage (Map message); void Handlemessage (byte[] message);
void Handlemessage (Serializable message);
void Handlemessage (Serializable message, String Channel);
}
First, you need a class that conforms to the Messagedelegate interface method signature, which is custom and can use one or more of the signature methods in Messagedelegate, such as:
public class Usermessagedelegate {public
void Handlemessage (String message) {
System.out.println (message);
}
}
Once this class is obtained, this class is registered in the Message listener container:
<?xml version= "1.0" encoding= "UTF-8"?> <beans
xmlns= "Http://www.springframework.org/schema/beans"
xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
xmlns:p= "http://www.springframework.org/schema/ P "
xmlns:redis=" Http://www.springframework.org/schema/redis "
xsi:schemalocation="/http/ Www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http ://www.springframework.org/schema/redis
http://www.springframework.org/schema/redis/spring-redis.xsd ">
<redis:listener-container>
<redis:listener ref= "Listener" method= "Handlemessage" topic= " Chatroom "/>
</redis:listener-container>
<bean id=" Listener "class=" Com.hzw.redis.listener.UserMessageDelegate "/>
</beans>
Among them, topic is the channel you want to listen to.