├── atoms └── src │ └── test │ ├── resources │ ├── atoms.properties │ ├── zookeeper_atoms.xml │ ├── redis-atoms.xml │ ├── log4j.properties │ ├── jgroup-atoms.xml │ ├── network.xml │ └── ehcache.xml │ └── java │ └── com │ └── lovver │ └── atoms │ ├── spring │ ├── User.java │ ├── UserService.java │ └── SpringCacheTest.java │ ├── GetClient1.java │ ├── SetClient.java │ ├── CacheManagerTest.java │ ├── CacheSpringContainerTest.java │ ├── CacheSingleKeyExpireTest.java │ ├── CacheSpringTest.java │ ├── SerializerFactoryTest.java │ ├── CommandTest.java │ └── SerializerTest.java ├── atoms-config ├── src │ ├── test │ │ ├── resources │ │ │ ├── atoms.properties │ │ │ ├── atoms.xml │ │ │ ├── ehcache.xml │ │ │ └── applicationContext.xml │ │ └── java │ │ │ ├── AtomsConfigTest.java │ │ │ └── SpringConfigTest.java │ └── main │ │ └── java │ │ └── com │ │ └── lovver │ │ └── atoms │ │ └── config │ │ ├── AtomsSpringConfig.java │ │ ├── AtomsSerializerBean.java │ │ ├── AtomsCacheTTLBean.java │ │ ├── AtomsBroadsetBean.java │ │ ├── AtomsCacheTTLConfigBean.java │ │ ├── AtomsBean.java │ │ ├── AtomsBroadCastBean.java │ │ ├── AtomsCacheBean.java │ │ ├── AtomsConfig.java │ │ └── AtomsCacheConfigBean.java └── pom.xml ├── atoms-springboot ├── atoms-spring-boot-starter │ ├── src │ │ └── main │ │ │ └── resources │ │ │ └── META-INF │ │ │ └── spring.providers │ └── pom.xml ├── atoms-spring-boot-autoconfigue │ ├── src │ │ └── main │ │ │ ├── resources │ │ │ └── META-INF │ │ │ │ └── spring.factories │ │ │ └── java │ │ │ └── com │ │ │ └── lovver │ │ │ └── atoms │ │ │ └── springboot │ │ │ └── autoconfigue │ │ │ ├── AtomsProperties.java │ │ │ └── AtomsAutoConfiguration.java │ └── pom.xml ├── atoms-spring-boot-test │ ├── src │ │ └── main │ │ │ ├── java │ │ │ └── com │ │ │ │ └── lovver │ │ │ │ └── atoms │ │ │ │ └── springboot │ │ │ │ ├── Application.java │ │ │ │ └── controller │ │ │ │ └── TestController.java │ │ │ └── resources │ │ │ ├── application.yaml │ │ │ ├── log4j.properties │ │ │ └── ehcache.xml │ └── pom.xml └── pom.xml ├── atoms-cache ├── atoms-cache-ssdb │ ├── src │ │ └── main │ │ │ ├── resources │ │ │ └── META-INF │ │ │ │ └── extension │ │ │ │ ├── com.lovver.atoms.cache.CacheProvider │ │ │ │ └── com.lovver.atoms.cache.CacheEventListener │ │ │ └── java │ │ │ └── com │ │ │ └── lovver │ │ │ └── atoms │ │ │ └── cache │ │ │ └── ssdb │ │ │ └── SsdbCacheEventListener.java │ └── pom.xml ├── atoms-cache-ehcache │ ├── src │ │ ├── main │ │ │ ├── resources │ │ │ │ └── META-INF │ │ │ │ │ └── extension │ │ │ │ │ ├── com.lovver.atoms.cache.CacheProvider │ │ │ │ │ └── com.lovver.atoms.cache.CacheEventListener │ │ │ └── java │ │ │ │ └── com │ │ │ │ └── lovver │ │ │ │ └── atoms │ │ │ │ └── cache │ │ │ │ └── ehcache │ │ │ │ ├── EhCacheEventListener.java │ │ │ │ └── EhCacheProvider.java │ │ └── test │ │ │ └── java │ │ │ └── TestEhcache.java │ └── pom.xml ├── atoms-cache-redis │ ├── src │ │ └── main │ │ │ ├── resources │ │ │ └── META-INF │ │ │ │ └── extension │ │ │ │ ├── com.lovver.atoms.cache.CacheProvider │ │ │ │ └── com.lovver.atoms.cache.CacheEventListener │ │ │ └── java │ │ │ └── com │ │ │ └── lovver │ │ │ └── atoms │ │ │ └── cache │ │ │ └── redis │ │ │ ├── RedisCacheEventListener.java │ │ │ └── RedisCacheProvider.java │ └── pom.xml ├── pom.xml └── atoms-cache-api │ ├── src │ └── main │ │ └── java │ │ └── com │ │ └── lovver │ │ └── atoms │ │ └── cache │ │ ├── CacheEventListenerFactory.java │ │ ├── CacheProviderFactory.java │ │ ├── CacheEventListener.java │ │ ├── CacheProvider.java │ │ └── Cache.java │ └── pom.xml ├── atoms-broadcast ├── atoms-broadcast-redis │ ├── src │ │ ├── main │ │ │ ├── resources │ │ │ │ └── META-INF │ │ │ │ │ └── extension │ │ │ │ │ └── com.lovver.atoms.broadcast.BroadCast │ │ │ └── java │ │ │ │ └── com │ │ │ │ └── lovver │ │ │ │ └── atoms │ │ │ │ └── broadcast │ │ │ │ └── redis │ │ │ │ ├── support │ │ │ │ └── SafeEncoder.java │ │ │ │ └── RedisBroadCast.java │ │ └── test │ │ │ └── java │ │ │ └── com │ │ │ └── sample │ │ │ └── redis │ │ │ ├── Constants.java │ │ │ ├── pubsub │ │ │ ├── persistent │ │ │ │ ├── PSubClient.java │ │ │ │ ├── PPubSubTestMain.java │ │ │ │ ├── PPubClient.java │ │ │ │ └── PPrintListener.java │ │ │ └── simple │ │ │ │ ├── PubClient.java │ │ │ │ ├── SubClient.java │ │ │ │ ├── PrintListener.java │ │ │ │ └── PubSubTestMain.java │ │ │ └── RedisClient.java │ └── pom.xml ├── atoms-broadcast-jgroups │ ├── src │ │ └── main │ │ │ ├── resources │ │ │ └── META-INF │ │ │ │ └── extension │ │ │ │ └── com.lovver.atoms.broadcast.BroadCast │ │ │ └── java │ │ │ └── com │ │ │ └── lovver │ │ │ └── atoms │ │ │ └── broadcast │ │ │ └── jgroups │ │ │ ├── JGroupsBroadCast.java │ │ │ └── JGroupsPubSub.java │ └── pom.xml ├── atoms-broadcast-zookeeper │ ├── src │ │ ├── main │ │ │ ├── resources │ │ │ │ └── META-INF │ │ │ │ │ └── extension │ │ │ │ │ └── com.lovver.atoms.broadcast.BroadCast │ │ │ └── java │ │ │ │ └── com │ │ │ │ └── lovver │ │ │ │ └── atoms │ │ │ │ └── broadcast │ │ │ │ └── zookeeper │ │ │ │ └── ZookeeperBroadCast.java │ │ └── test │ │ │ └── java │ │ │ └── com │ │ │ └── sample │ │ │ └── ZookeeperTest.java │ └── pom.xml ├── atoms-broadcast-api │ ├── src │ │ └── main │ │ │ └── java │ │ │ └── com │ │ │ └── lovver │ │ │ └── atoms │ │ │ └── broadcast │ │ │ ├── BroadCast.java │ │ │ └── BroadCastFactory.java │ └── pom.xml └── pom.xml ├── atoms-serializer ├── atoms-serializer-fst │ ├── src │ │ └── main │ │ │ ├── resources │ │ │ └── META-INF │ │ │ │ └── extension │ │ │ │ └── com.lovver.atoms.serializer.Serializer │ │ │ └── java │ │ │ └── com │ │ │ └── lovver │ │ │ └── atoms │ │ │ └── serializer │ │ │ └── fst │ │ │ └── FSTSerializer.java │ └── pom.xml ├── atoms-serializer-kryo │ ├── src │ │ └── main │ │ │ ├── resources │ │ │ └── META-INF │ │ │ │ └── extension │ │ │ │ └── com.lovver.atoms.serializer.Serializer │ │ │ └── java │ │ │ └── com │ │ │ └── lovver │ │ │ └── atoms │ │ │ └── serializer │ │ │ └── kryo │ │ │ └── KryoSerializer.java │ └── pom.xml ├── atoms-serializer-java │ ├── src │ │ └── main │ │ │ ├── resources │ │ │ └── META-INF │ │ │ │ └── extension │ │ │ │ └── com.lovver.atoms.serializer.Serializer │ │ │ └── java │ │ │ └── com │ │ │ └── lovver │ │ │ └── atoms │ │ │ └── serializer │ │ │ └── java │ │ │ └── JavaSerializer.java │ └── pom.xml ├── atoms-serializer-kryopool │ ├── src │ │ └── main │ │ │ ├── resources │ │ │ └── META-INF │ │ │ │ └── extension │ │ │ │ └── com.lovver.atoms.serializer.Serializer │ │ │ └── java │ │ │ └── com │ │ │ └── lovver │ │ │ └── atoms │ │ │ └── serializer │ │ │ └── kryopool │ │ │ └── KryoPoolSerializer.java │ └── pom.xml ├── atoms-serializer-api │ ├── src │ │ └── main │ │ │ └── java │ │ │ └── com │ │ │ └── lovver │ │ │ └── atoms │ │ │ └── serializer │ │ │ ├── Serializer.java │ │ │ └── SerializerFactory.java │ └── pom.xml └── pom.xml ├── atoms-core ├── src │ └── main │ │ └── java │ │ └── com │ │ └── lovver │ │ └── atoms │ │ └── core │ │ ├── ExpireOperator.java │ │ ├── CacheManager.java │ │ └── CacheChannel.java └── pom.xml ├── atoms-common ├── src │ └── main │ │ └── java │ │ └── com │ │ └── lovver │ │ └── atoms │ │ └── common │ │ ├── utils │ │ ├── Holder.java │ │ └── ReflectionUtils.java │ │ ├── annotation │ │ ├── SPI.java │ │ └── Extension.java │ │ └── exception │ │ └── CacheException.java └── pom.xml ├── .gitignore ├── atoms-spring ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── lovver │ └── atoms │ └── spring │ └── cache │ └── SpringAtomsCache.java ├── Readme.md ├── atoms-context ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── lovver │ └── atoms │ └── context │ └── AtomsContext.java └── pom.xml /atoms/src/test/resources/atoms.properties: -------------------------------------------------------------------------------- 1 | cache.2.host=127.0.0.1 2 | cache.2.port=6379 3 | cache.2.timeout=2233 4 | -------------------------------------------------------------------------------- /atoms-config/src/test/resources/atoms.properties: -------------------------------------------------------------------------------- 1 | cache.2.host=192.168.1.222 2 | cache.2.port=2222 3 | cache.2.timeout=2233 4 | -------------------------------------------------------------------------------- /atoms-springboot/atoms-spring-boot-starter/src/main/resources/META-INF/spring.providers: -------------------------------------------------------------------------------- 1 | provides: atoms-spring-boot-autoconfigue,atoms -------------------------------------------------------------------------------- /atoms-cache/atoms-cache-ssdb/src/main/resources/META-INF/extension/com.lovver.atoms.cache.CacheProvider: -------------------------------------------------------------------------------- 1 | com.lovver.atoms.cache.ssdb.SsdbCacheProvider 2 | -------------------------------------------------------------------------------- /atoms-cache/atoms-cache-ehcache/src/main/resources/META-INF/extension/com.lovver.atoms.cache.CacheProvider: -------------------------------------------------------------------------------- 1 | com.lovver.atoms.cache.ehcache.EhCacheProvider 2 | -------------------------------------------------------------------------------- /atoms-cache/atoms-cache-redis/src/main/resources/META-INF/extension/com.lovver.atoms.cache.CacheProvider: -------------------------------------------------------------------------------- 1 | com.lovver.atoms.cache.redis.RedisCacheProvider 2 | -------------------------------------------------------------------------------- /atoms-cache/atoms-cache-ssdb/src/main/resources/META-INF/extension/com.lovver.atoms.cache.CacheEventListener: -------------------------------------------------------------------------------- 1 | com.lovver.atoms.cache.ssdb.SsdbCacheEventListener 2 | -------------------------------------------------------------------------------- /atoms-broadcast/atoms-broadcast-redis/src/main/resources/META-INF/extension/com.lovver.atoms.broadcast.BroadCast: -------------------------------------------------------------------------------- 1 | com.lovver.atoms.broadcast.redis.RedisBroadCast 2 | -------------------------------------------------------------------------------- /atoms-cache/atoms-cache-ehcache/src/main/resources/META-INF/extension/com.lovver.atoms.cache.CacheEventListener: -------------------------------------------------------------------------------- 1 | com.lovver.atoms.cache.ehcache.EhCacheEventListener 2 | -------------------------------------------------------------------------------- /atoms-cache/atoms-cache-redis/src/main/resources/META-INF/extension/com.lovver.atoms.cache.CacheEventListener: -------------------------------------------------------------------------------- 1 | com.lovver.atoms.cache.redis.RedisCacheEventListener 2 | -------------------------------------------------------------------------------- /atoms-serializer/atoms-serializer-fst/src/main/resources/META-INF/extension/com.lovver.atoms.serializer.Serializer: -------------------------------------------------------------------------------- 1 | com.lovver.atoms.serializer.fst.FSTSerializer 2 | -------------------------------------------------------------------------------- /atoms-serializer/atoms-serializer-kryo/src/main/resources/META-INF/extension/com.lovver.atoms.serializer.Serializer: -------------------------------------------------------------------------------- 1 | com.lovver.atoms.serializer.kryo.KryoSerializer 2 | -------------------------------------------------------------------------------- /atoms-broadcast/atoms-broadcast-jgroups/src/main/resources/META-INF/extension/com.lovver.atoms.broadcast.BroadCast: -------------------------------------------------------------------------------- 1 | com.lovver.atoms.broadcast.jgroups.JGroupsBroadCast 2 | -------------------------------------------------------------------------------- /atoms-core/src/main/java/com/lovver/atoms/core/ExpireOperator.java: -------------------------------------------------------------------------------- 1 | package com.lovver.atoms.core; 2 | 3 | public enum ExpireOperator { 4 | DELETE,UPDATE; 5 | } 6 | -------------------------------------------------------------------------------- /atoms-serializer/atoms-serializer-java/src/main/resources/META-INF/extension/com.lovver.atoms.serializer.Serializer: -------------------------------------------------------------------------------- 1 | com.lovver.atoms.serializer.java.JavaSerializer 2 | 3 | -------------------------------------------------------------------------------- /atoms-broadcast/atoms-broadcast-zookeeper/src/main/resources/META-INF/extension/com.lovver.atoms.broadcast.BroadCast: -------------------------------------------------------------------------------- 1 | com.lovver.atoms.broadcast.zookeeper.ZookeeperBroadCast 2 | -------------------------------------------------------------------------------- /atoms-serializer/atoms-serializer-kryopool/src/main/resources/META-INF/extension/com.lovver.atoms.serializer.Serializer: -------------------------------------------------------------------------------- 1 | com.lovver.atoms.serializer.kryopool.KryoPoolSerializer 2 | 3 | -------------------------------------------------------------------------------- /atoms-springboot/atoms-spring-boot-autoconfigue/src/main/resources/META-INF/spring.factories: -------------------------------------------------------------------------------- 1 | org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ 2 | com.lovver.atoms.springboot.autoconfigue.AtomsAutoConfiguration -------------------------------------------------------------------------------- /atoms-config/src/main/java/com/lovver/atoms/config/AtomsSpringConfig.java: -------------------------------------------------------------------------------- 1 | package com.lovver.atoms.config; 2 | 3 | /** 4 | * Created by Administrator on 2017/3/15. 5 | */ 6 | public class AtomsSpringConfig extends AtomsConfig{ 7 | 8 | public void setAtomsBean(AtomsBean atomsBean){ 9 | AtomsConfig.atomsBean=atomsBean; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /atoms-common/src/main/java/com/lovver/atoms/common/utils/Holder.java: -------------------------------------------------------------------------------- 1 | package com.lovver.atoms.common.utils; 2 | 3 | public class Holder { 4 | 5 | private volatile T value; 6 | 7 | public void set(T value) { 8 | this.value = value; 9 | } 10 | 11 | public T get() { 12 | return value; 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /atoms-springboot/atoms-spring-boot-autoconfigue/src/main/java/com/lovver/atoms/springboot/autoconfigue/AtomsProperties.java: -------------------------------------------------------------------------------- 1 | package com.lovver.atoms.springboot.autoconfigue; 2 | 3 | import com.lovver.atoms.config.AtomsBean; 4 | import org.springframework.boot.context.properties.ConfigurationProperties; 5 | 6 | @ConfigurationProperties(prefix = "atoms") 7 | public class AtomsProperties extends AtomsBean{ 8 | 9 | } -------------------------------------------------------------------------------- /atoms-broadcast/atoms-broadcast-redis/src/test/java/com/sample/redis/Constants.java: -------------------------------------------------------------------------------- 1 | package com.sample.redis; 2 | 3 | public class Constants { 4 | 5 | public static final String host = "192.168.1.53"; 6 | public static final int port = 6379; 7 | 8 | public static final String SUBSCRIBE_CENTER = "_-subscribe-center-_"; 9 | 10 | public static final String MESSAGE_TXID = "_-message-txid-_"; 11 | 12 | } 13 | -------------------------------------------------------------------------------- /atoms-common/src/main/java/com/lovver/atoms/common/annotation/SPI.java: -------------------------------------------------------------------------------- 1 | package com.lovver.atoms.common.annotation; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | @Target(ElementType.TYPE) 9 | @Retention(RetentionPolicy.RUNTIME) 10 | public @interface SPI { 11 | String value(); 12 | } -------------------------------------------------------------------------------- /atoms-config/src/test/java/AtomsConfigTest.java: -------------------------------------------------------------------------------- 1 | import com.lovver.atoms.config.AtomsBean; 2 | import com.lovver.atoms.config.AtomsConfig; 3 | 4 | /** 5 | * Created by Administrator on 2017/3/23. 6 | */ 7 | public class AtomsConfigTest { 8 | 9 | 10 | public static void main(String args[]){ 11 | AtomsBean config= AtomsConfig.getAtomsConfig(); 12 | System.out.println(config.getSerializer()); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /atoms-common/src/main/java/com/lovver/atoms/common/annotation/Extension.java: -------------------------------------------------------------------------------- 1 | package com.lovver.atoms.common.annotation; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | @Target(ElementType.TYPE) 9 | @Retention(RetentionPolicy.RUNTIME) 10 | public @interface Extension { 11 | String value(); 12 | } -------------------------------------------------------------------------------- /atoms-broadcast/atoms-broadcast-api/src/main/java/com/lovver/atoms/broadcast/BroadCast.java: -------------------------------------------------------------------------------- 1 | package com.lovver.atoms.broadcast; 2 | 3 | import com.lovver.atoms.common.annotation.Extension; 4 | import com.lovver.atoms.config.AtomsBroadCastBean; 5 | 6 | @Extension("spi") 7 | public interface BroadCast extends Cloneable{ 8 | 9 | public void init(AtomsBroadCastBean atomBean); 10 | 11 | public void broadcast(byte[] message); 12 | } 13 | -------------------------------------------------------------------------------- /atoms-common/src/main/java/com/lovver/atoms/common/exception/CacheException.java: -------------------------------------------------------------------------------- 1 | package com.lovver.atoms.common.exception; 2 | 3 | /** 4 | * 缓存相关的异常 5 | * @author jobell 6 | */ 7 | @SuppressWarnings("serial") 8 | public class CacheException extends RuntimeException { 9 | 10 | public CacheException(String s) { 11 | super(s); 12 | } 13 | 14 | public CacheException(String s, Throwable e) { 15 | super(s, e); 16 | } 17 | 18 | public CacheException(Throwable e) { 19 | super(e); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /atoms-config/src/main/java/com/lovver/atoms/config/AtomsSerializerBean.java: -------------------------------------------------------------------------------- 1 | package com.lovver.atoms.config; 2 | 3 | import com.thoughtworks.xstream.annotations.XStreamAlias; 4 | import com.thoughtworks.xstream.annotations.XStreamAsAttribute; 5 | 6 | @XStreamAlias("serializer") 7 | public class AtomsSerializerBean { 8 | 9 | @XStreamAsAttribute 10 | private String type; 11 | 12 | public String getType() { 13 | return type; 14 | } 15 | 16 | public void setType(String type) { 17 | this.type = type; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /atoms-serializer/atoms-serializer-api/src/main/java/com/lovver/atoms/serializer/Serializer.java: -------------------------------------------------------------------------------- 1 | package com.lovver.atoms.serializer; 2 | 3 | import java.io.IOException; 4 | 5 | import com.lovver.atoms.common.annotation.Extension; 6 | 7 | /** 8 | * 对象序列化接口 9 | * @author jobell 10 | */ 11 | @Extension("spi") 12 | public interface Serializer { 13 | 14 | public String name(); 15 | 16 | public byte[] serialize(Object obj) throws IOException ; 17 | 18 | public Object deserialize(byte[] bytes) throws IOException ; 19 | 20 | } 21 | -------------------------------------------------------------------------------- /atoms-config/src/test/java/SpringConfigTest.java: -------------------------------------------------------------------------------- 1 | import com.lovver.atoms.config.AtomsBean; 2 | import com.lovver.atoms.config.AtomsConfig; 3 | import org.springframework.context.ApplicationContext; 4 | import org.springframework.context.support.ClassPathXmlApplicationContext; 5 | 6 | 7 | public class SpringConfigTest { 8 | 9 | public static void main(String[] args) { 10 | ApplicationContext ac=new ClassPathXmlApplicationContext(new String[]{"classpath:applicationContext.xml"}); 11 | AtomsBean config=AtomsConfig.getAtomsConfig(); 12 | System.out.println(config.getSerializer()); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /atoms-config/src/main/java/com/lovver/atoms/config/AtomsCacheTTLBean.java: -------------------------------------------------------------------------------- 1 | package com.lovver.atoms.config; 2 | 3 | import java.util.List; 4 | 5 | import com.thoughtworks.xstream.annotations.XStreamAlias; 6 | import com.thoughtworks.xstream.annotations.XStreamImplicit; 7 | 8 | @XStreamAlias("cacheTTL") 9 | public class AtomsCacheTTLBean { 10 | 11 | @XStreamImplicit(itemFieldName = "ttl") 12 | private List lstTTL; 13 | 14 | public List getLstTTL() { 15 | return lstTTL; 16 | } 17 | 18 | public void setLstTTL(List lstTTL) { 19 | this.lstTTL = lstTTL; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /atoms-springboot/atoms-spring-boot-test/src/main/java/com/lovver/atoms/springboot/Application.java: -------------------------------------------------------------------------------- 1 | package com.lovver.atoms.springboot; 2 | 3 | import com.lovver.atoms.springboot.autoconfigue.AtomsProperties; 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | import org.springframework.boot.context.properties.EnableConfigurationProperties; 7 | import org.springframework.cache.annotation.EnableCaching; 8 | 9 | 10 | @SpringBootApplication 11 | public class Application { 12 | 13 | public static void main(String[] args) { 14 | SpringApplication.run(Application.class, args); 15 | } 16 | 17 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | 3 | # Package Files # 4 | *.jar 5 | *.war 6 | *.ear 7 | *.zip 8 | 9 | # other dir # 10 | classes/ 11 | .temp/ 12 | logs/ 13 | log/ 14 | .svn/ 15 | svn/ 16 | lib/ 17 | 18 | # maven # 19 | target/ 20 | 21 | # intellj file type # 22 | .idea/ 23 | .svn/ 24 | bin/ 25 | *.iml 26 | 27 | # eclipse file type # 28 | .settings/ 29 | .classpath 30 | .project 31 | 32 | # ---- Mac OS X 33 | .DS_Store 34 | Icon? 35 | 36 | # ---- Windows 37 | 38 | # Windows image file caches 39 | Thumbs.db 40 | 41 | # Folder config file 42 | Desktop.ini 43 | 44 | # ---- Javadoc 45 | docs.tar 46 | 47 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 48 | hs_err_pid* -------------------------------------------------------------------------------- /atoms-broadcast/atoms-broadcast-redis/src/test/java/com/sample/redis/pubsub/persistent/PSubClient.java: -------------------------------------------------------------------------------- 1 | package com.sample.redis.pubsub.persistent; 2 | 3 | import redis.clients.jedis.Jedis; 4 | import redis.clients.jedis.JedisPubSub; 5 | 6 | public class PSubClient { 7 | 8 | private Jedis jedis;// 9 | private JedisPubSub listener;//单listener 10 | 11 | public PSubClient(String host,int port,String clientId){ 12 | jedis = new Jedis(host,port); 13 | listener = new PPrintListener(clientId, new Jedis(host, port)); 14 | } 15 | 16 | public void sub(String channel){ 17 | jedis.subscribe(listener, channel); 18 | } 19 | 20 | public void unsubscribe(String channel){ 21 | listener.unsubscribe(channel); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /atoms-broadcast/atoms-broadcast-redis/src/test/java/com/sample/redis/pubsub/simple/PubClient.java: -------------------------------------------------------------------------------- 1 | package com.sample.redis.pubsub.simple; 2 | 3 | import redis.clients.jedis.Jedis; 4 | 5 | public class PubClient { 6 | 7 | private Jedis jedis;// 8 | public PubClient(String host,int port){ 9 | jedis = new Jedis(host,port); 10 | } 11 | 12 | public void pub(String channel,String message){ 13 | jedis.publish(channel, message); 14 | } 15 | 16 | public void close(String channel){ 17 | jedis.publish(channel, "quit"); 18 | jedis.del(channel);// 19 | } 20 | 21 | public void test(){ 22 | jedis.set("pub-block", "15"); 23 | String tmp = jedis.get("pub-block"); 24 | System.out.println("TEST:" + tmp); 25 | } 26 | 27 | 28 | } 29 | -------------------------------------------------------------------------------- /atoms/src/test/java/com/lovver/atoms/spring/User.java: -------------------------------------------------------------------------------- 1 | package com.lovver.atoms.spring; 2 | 3 | import java.io.Serializable; 4 | 5 | //缓存对象 6 | public class User implements Serializable{ 7 | /** 8 | * 9 | */ 10 | private static final long serialVersionUID = 1L; 11 | private int id; 12 | private String name; 13 | 14 | public User(){ 15 | } 16 | 17 | public User(String name){ 18 | this.name= name; 19 | } 20 | 21 | public int getId() { 22 | return id; 23 | } 24 | public void setId(int id) { 25 | this.id = id; 26 | } 27 | public String getName() { 28 | return name; 29 | } 30 | public void setName(String name) { 31 | this.name = name; 32 | } 33 | } -------------------------------------------------------------------------------- /atoms-cache/atoms-cache-ehcache/src/test/java/TestEhcache.java: -------------------------------------------------------------------------------- 1 | import net.sf.ehcache.CacheManager; 2 | import net.sf.ehcache.Ehcache; 3 | import net.sf.ehcache.Element; 4 | 5 | /** 6 | * Created by Administrator on 2017/3/15. 7 | */ 8 | public class TestEhcache { 9 | public static void main(String args[]) throws InterruptedException { 10 | CacheManager ehCacheManager = new CacheManager(); 11 | ehCacheManager.addCache("default-111"); 12 | Ehcache cache = ehCacheManager.getCache("default-111"); 13 | Element e = new Element("aa", "aa", false, 1, 1); 14 | cache.put(e); 15 | System.out.println(cache.get("aa")); 16 | Thread.sleep(1050); 17 | System.out.println(cache.get("aa")); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /atoms-cache/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.lovver.atoms 6 | atoms-parent 7 | 1.0.18-SNAPSHOT 8 | 9 | atoms-cache 10 | pom 11 | 12 | 13 | atoms-cache-api 14 | atoms-cache-redis 15 | atoms-cache-ehcache 16 | atoms-cache-ssdb 17 | 18 | -------------------------------------------------------------------------------- /atoms-broadcast/atoms-broadcast-redis/src/test/java/com/sample/redis/pubsub/simple/SubClient.java: -------------------------------------------------------------------------------- 1 | package com.sample.redis.pubsub.simple; 2 | 3 | import redis.clients.jedis.Jedis; 4 | import redis.clients.jedis.JedisPubSub; 5 | 6 | public class SubClient { 7 | 8 | private Jedis jedis;// 9 | 10 | public SubClient(String host,int port){ 11 | jedis = new Jedis(host,port); 12 | } 13 | 14 | public void sub(JedisPubSub listener,String channel){ 15 | jedis.subscribe(listener, channel); 16 | //此处将会阻塞,在client代码级别为JedisPubSub在处理消息时,将会“独占”链接 17 | //并且采取了while循环的方式,侦听订阅的消息 18 | // 19 | } 20 | 21 | 22 | public void test(){ 23 | jedis.set("sub-block", "15"); 24 | System.out.println("TEST"); 25 | //String tmp = jedis.get("sub-block"); 26 | //System.out.println("TEST:" + tmp); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /atoms-cache/atoms-cache-api/src/main/java/com/lovver/atoms/cache/CacheEventListenerFactory.java: -------------------------------------------------------------------------------- 1 | package com.lovver.atoms.cache; 2 | 3 | import com.lovver.atoms.common.extension.ExtensionLoader; 4 | 5 | public class CacheEventListenerFactory { 6 | private static ExtensionLoader exloader = ExtensionLoader.getExtensionLoader(CacheEventListener.class); 7 | 8 | public static CacheEventListener getCacheEventListener(String type,int level) { 9 | try{ 10 | CacheEventListener cacheEventListener = exloader.getExtension(type); 11 | if(null==cacheEventListener){ 12 | return cacheEventListener; 13 | } 14 | cacheEventListener.init(level); 15 | return cacheEventListener; 16 | }catch(Exception e){ 17 | e.printStackTrace(); 18 | } 19 | return null; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /atoms-config/src/main/java/com/lovver/atoms/config/AtomsBroadsetBean.java: -------------------------------------------------------------------------------- 1 | package com.lovver.atoms.config; 2 | 3 | import com.thoughtworks.xstream.annotations.XStreamAlias; 4 | import com.thoughtworks.xstream.annotations.XStreamAsAttribute; 5 | 6 | /** 7 | * Created by Administrator on 2017/3/23. 8 | */ 9 | @XStreamAlias("broadset") 10 | public class AtomsBroadsetBean { 11 | @XStreamAsAttribute 12 | private String region; 13 | @XStreamAsAttribute 14 | private String key; 15 | 16 | public String getRegion() { 17 | return region; 18 | } 19 | 20 | public void setRegion(String region) { 21 | this.region = region; 22 | } 23 | 24 | public String getKey() { 25 | return key; 26 | } 27 | 28 | public void setKey(String key) { 29 | this.key = key; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /atoms-springboot/atoms-spring-boot-test/src/main/java/com/lovver/atoms/springboot/controller/TestController.java: -------------------------------------------------------------------------------- 1 | package com.lovver.atoms.springboot.controller; 2 | 3 | import com.lovver.atoms.core.CacheChannel; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.stereotype.Controller; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.ResponseBody; 8 | 9 | @Controller 10 | public class TestController { 11 | @Autowired 12 | private CacheChannel cacheChannel; 13 | @RequestMapping("/test") 14 | @ResponseBody 15 | public String test(){ 16 | cacheChannel.set("hello","jobell","test"); 17 | System.out.println(cacheChannel.get("hello","jobell")); 18 | return "hello"; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /atoms-springboot/atoms-spring-boot-starter/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.lovver.atoms 6 | atoms-springboot 7 | 1.0.18-SNAPSHOT 8 | 9 | atoms-spring-boot-starter 10 | 11 | 12 | com.lovver.atoms 13 | atoms-spring-boot-autoconfigue 14 | 1.0.18-SNAPSHOT 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /atoms-config/src/main/java/com/lovver/atoms/config/AtomsCacheTTLConfigBean.java: -------------------------------------------------------------------------------- 1 | package com.lovver.atoms.config; 2 | 3 | import com.thoughtworks.xstream.annotations.XStreamAsAttribute; 4 | 5 | public class AtomsCacheTTLConfigBean { 6 | @XStreamAsAttribute 7 | private String name; 8 | @XStreamAsAttribute 9 | private String value; 10 | @XStreamAsAttribute 11 | private String broadset;//set时是否广播 12 | 13 | public String getName() { 14 | return name; 15 | } 16 | 17 | public void setName(String name) { 18 | this.name = name; 19 | } 20 | 21 | public String getValue() { 22 | return value; 23 | } 24 | 25 | public void setValue(String value) { 26 | this.value = value; 27 | } 28 | 29 | public String getBroadset() { 30 | return broadset; 31 | } 32 | 33 | public void setBroadset(String broadset) { 34 | this.broadset = broadset; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /atoms-broadcast/atoms-broadcast-api/src/main/java/com/lovver/atoms/broadcast/BroadCastFactory.java: -------------------------------------------------------------------------------- 1 | package com.lovver.atoms.broadcast; 2 | 3 | import com.lovver.atoms.common.extension.ExtensionLoader; 4 | import com.lovver.atoms.common.utils.StringUtils; 5 | import com.lovver.atoms.config.AtomsBroadCastBean; 6 | 7 | public class BroadCastFactory { 8 | private static ExtensionLoader exloader = ExtensionLoader 9 | .getExtensionLoader(BroadCast.class); 10 | 11 | public static BroadCast getBroadCast(AtomsBroadCastBean atomBean) 12 | throws InstantiationException, IllegalAccessException { 13 | String type = atomBean.getType(); 14 | if (StringUtils.isEmpty(type)) { 15 | type = "redis"; 16 | } 17 | BroadCast broadCast = exloader.getExtension(type); 18 | broadCast.init(atomBean); 19 | return broadCast; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /atoms-cache/atoms-cache-api/src/main/java/com/lovver/atoms/cache/CacheProviderFactory.java: -------------------------------------------------------------------------------- 1 | package com.lovver.atoms.cache; 2 | 3 | import com.lovver.atoms.common.extension.ExtensionLoader; 4 | import com.lovver.atoms.common.utils.StringUtils; 5 | import com.lovver.atoms.config.AtomsCacheBean; 6 | 7 | public class CacheProviderFactory { 8 | private static ExtensionLoader exloader = ExtensionLoader.getExtensionLoader(CacheProvider.class); 9 | 10 | public static CacheProvider getCacheProvider(AtomsCacheBean atomBean,int level) 11 | throws InstantiationException, IllegalAccessException { 12 | String type = atomBean.getType(); 13 | if (StringUtils.isEmpty(type)) { 14 | throw new RuntimeException("plese give the CacheProvider type"); 15 | } 16 | CacheProvider cacheProvider = exloader.getExtension(type,level); 17 | cacheProvider.start(atomBean); 18 | return cacheProvider; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /atoms/src/test/java/com/lovver/atoms/GetClient1.java: -------------------------------------------------------------------------------- 1 | package com.lovver.atoms; 2 | 3 | 4 | import com.lovver.atoms.core.CacheChannel; 5 | import org.junit.Test; 6 | import org.junit.runner.RunWith; 7 | import org.springframework.test.context.ContextConfiguration; 8 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 9 | import org.springframework.test.context.transaction.TransactionConfiguration; 10 | 11 | @RunWith(SpringJUnit4ClassRunner.class) 12 | @ContextConfiguration(value = {"classpath:applicationContext.xml"}) 13 | @TransactionConfiguration(defaultRollback = false) 14 | public class GetClient1 { 15 | 16 | @Test 17 | public void main() throws InterruptedException { 18 | CacheChannel cc=CacheChannel.getInstance(); 19 | 20 | while(true){ 21 | Object dd=cc.get("hello", "dddd"); 22 | System.out.println("=============== CacheChannelTest2"+dd); 23 | Thread.sleep(1000); 24 | } 25 | 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /atoms/src/test/java/com/lovver/atoms/SetClient.java: -------------------------------------------------------------------------------- 1 | package com.lovver.atoms; 2 | 3 | 4 | import com.lovver.atoms.core.CacheChannel; 5 | import org.junit.Test; 6 | import org.junit.runner.RunWith; 7 | import org.springframework.test.context.ContextConfiguration; 8 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 9 | import org.springframework.test.context.transaction.TransactionConfiguration; 10 | 11 | @RunWith(SpringJUnit4ClassRunner.class) 12 | @ContextConfiguration(value = {"classpath:applicationContext.xml"}) 13 | @TransactionConfiguration(defaultRollback = false) 14 | public class SetClient { 15 | 16 | @Test 17 | public void main() throws InterruptedException { 18 | CacheChannel cc=CacheChannel.getInstance(); 19 | for(int i=0;i<10;i++){ 20 | cc.set("hello", "dddd", "jobell_"+i,1); 21 | System.out.println("==============="+cc.get("hello", "dddd")); 22 | Thread.sleep(2000); 23 | } 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /atoms/src/test/resources/zookeeper_atoms.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 18 | 19 | -------------------------------------------------------------------------------- /atoms/src/test/java/com/lovver/atoms/CacheManagerTest.java: -------------------------------------------------------------------------------- 1 | package com.lovver.atoms; 2 | 3 | import com.lovver.atoms.cache.Cache; 4 | 5 | import com.lovver.atoms.core.CacheManager; 6 | import org.junit.Test; 7 | import org.junit.runner.RunWith; 8 | import org.springframework.test.context.ContextConfiguration; 9 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 10 | import org.springframework.test.context.transaction.TransactionConfiguration; 11 | 12 | @RunWith(SpringJUnit4ClassRunner.class) 13 | @ContextConfiguration(value = {"classpath:applicationContext.xml"}) 14 | @TransactionConfiguration(defaultRollback = false) 15 | public class CacheManagerTest { 16 | 17 | @Test 18 | public void main() { 19 | Cache tCache= CacheManager.getCache(1, "hello", true); 20 | for(int i=0;i<10000;i++){ 21 | tCache.put("test"+i, "wahahaha"+i); 22 | } 23 | 24 | 25 | for(int i=0;i<10000;i++){ 26 | System.out.println("==============="+tCache.get("test"+i)); 27 | } 28 | 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /atoms/src/test/java/com/lovver/atoms/CacheSpringContainerTest.java: -------------------------------------------------------------------------------- 1 | package com.lovver.atoms; 2 | 3 | 4 | import com.lovver.atoms.core.CacheChannel; 5 | import org.junit.Test; 6 | import org.junit.runner.RunWith; 7 | import org.springframework.test.context.ContextConfiguration; 8 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 9 | import org.springframework.test.context.transaction.TransactionConfiguration; 10 | 11 | /** 12 | * Created by Administrator on 2017/3/20. 13 | */ 14 | @RunWith(SpringJUnit4ClassRunner.class) 15 | @ContextConfiguration(value = {"classpath:applicationContext.xml"}) 16 | @TransactionConfiguration(defaultRollback = false) 17 | public class CacheSpringContainerTest { 18 | 19 | @Test 20 | public void run() throws InterruptedException { 21 | CacheChannel cc = CacheChannel.getInstance(); 22 | 23 | cc.set("test", "dddd1", "jobell" ); 24 | String val=(String)cc.get("test","dddd1"); 25 | assert ("jobell".equals(val)); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /atoms-broadcast/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | 4 | com.lovver.atoms 5 | atoms-parent 6 | 1.0.18-SNAPSHOT 7 | 8 | atoms-broadcast 9 | pom 10 | 11 | 12 | 13 | maven-compiler-plugin 14 | 15 | 1.7 16 | 1.7 17 | 18 | 19 | 20 | 21 | 22 | atoms-broadcast-api 23 | atoms-broadcast-redis 24 | atoms-broadcast-jgroups 25 | atoms-broadcast-zookeeper 26 | 27 | -------------------------------------------------------------------------------- /atoms/src/test/java/com/lovver/atoms/CacheSingleKeyExpireTest.java: -------------------------------------------------------------------------------- 1 | package com.lovver.atoms; 2 | 3 | 4 | import com.lovver.atoms.core.CacheChannel; 5 | import org.junit.Test; 6 | import org.junit.runner.RunWith; 7 | import org.springframework.test.context.ContextConfiguration; 8 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 9 | import org.springframework.test.context.transaction.TransactionConfiguration; 10 | 11 | /** 12 | * Created by Administrator on 2017/3/20. 13 | */ 14 | @RunWith(SpringJUnit4ClassRunner.class) 15 | @ContextConfiguration(value = {"classpath:applicationContext.xml"}) 16 | @TransactionConfiguration(defaultRollback = false) 17 | public class CacheSingleKeyExpireTest { 18 | 19 | @Test 20 | public void run() throws InterruptedException { 21 | CacheChannel cc = CacheChannel.getInstance(); 22 | cc.set("test", "dddd1", "jobell", 1); 23 | 24 | Thread.sleep(2000); 25 | Object value = cc.get("test", "dddd1"); 26 | assert (value == null); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /atoms-broadcast/atoms-broadcast-zookeeper/src/main/java/com/lovver/atoms/broadcast/zookeeper/ZookeeperBroadCast.java: -------------------------------------------------------------------------------- 1 | package com.lovver.atoms.broadcast.zookeeper; 2 | 3 | import com.lovver.atoms.broadcast.BroadCast; 4 | import com.lovver.atoms.common.annotation.SPI; 5 | import com.lovver.atoms.config.AtomsBroadCastBean; 6 | import org.apache.commons.lang.StringUtils; 7 | import org.apache.zookeeper.CreateMode; 8 | 9 | @SPI("zookeeper") 10 | public class ZookeeperBroadCast implements BroadCast{ 11 | 12 | private static String channel_prefix="atoms_channel"; 13 | private String channel = null; 14 | 15 | private ZookeeperPubSub zkPubSub; 16 | 17 | public void init(AtomsBroadCastBean broadcastBean){ 18 | if(StringUtils.isNotEmpty(broadcastBean.getChannel())) { 19 | channel = channel_prefix+"_"+broadcastBean.getChannel(); 20 | }else{ 21 | channel = channel_prefix; 22 | } 23 | zkPubSub=new ZookeeperPubSub(broadcastBean,this.channel); 24 | } 25 | 26 | public void broadcast(byte[] message){ 27 | zkPubSub.pub(message); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /atoms/src/test/java/com/lovver/atoms/CacheSpringTest.java: -------------------------------------------------------------------------------- 1 | package com.lovver.atoms; 2 | 3 | 4 | import com.lovver.atoms.core.CacheChannel; 5 | import org.springframework.context.ApplicationContext; 6 | import org.springframework.context.support.ClassPathXmlApplicationContext; 7 | 8 | /** 9 | * Created by Administrator on 2017/3/20. 10 | */ 11 | public class CacheSpringTest { 12 | 13 | public static void main(String[] args) throws InterruptedException { 14 | ApplicationContext ac=new ClassPathXmlApplicationContext(new String[]{"classpath:applicationContext.xml"}); 15 | //AtomsBean config= AtomsConfig.getAtomsConfig(); 16 | //System.out.println(config.getSerializer()); 17 | 18 | CacheChannel cc=CacheChannel.getInstance(); 19 | 20 | while(true){ 21 | //cc.set("test", "dddd1", "jobell_"+i); 22 | System.out.println("==============="+cc.get("test", "dddd1")); 23 | Thread.sleep(1000); 24 | } 25 | 26 | // System.exit(0); 27 | // cc.evict("hello","dddd"); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /atoms-serializer/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | 4 | com.lovver.atoms 5 | atoms-parent 6 | 1.0.18-SNAPSHOT 7 | 8 | atoms-serializer 9 | pom 10 | 11 | 12 | 13 | maven-compiler-plugin 14 | 15 | 1.7 16 | 1.7 17 | 18 | 19 | 20 | 21 | 22 | atoms-serializer-api 23 | atoms-serializer-fst 24 | atoms-serializer-java 25 | atoms-serializer-kryo 26 | atoms-serializer-kryopool 27 | 28 | 29 | -------------------------------------------------------------------------------- /atoms-broadcast/atoms-broadcast-jgroups/src/main/java/com/lovver/atoms/broadcast/jgroups/JGroupsBroadCast.java: -------------------------------------------------------------------------------- 1 | package com.lovver.atoms.broadcast.jgroups; 2 | 3 | import com.lovver.atoms.broadcast.BroadCast; 4 | import com.lovver.atoms.common.annotation.SPI; 5 | import com.lovver.atoms.config.AtomsBroadCastBean; 6 | import org.apache.commons.lang.StringUtils; 7 | 8 | @SPI("jgroups") 9 | public class JGroupsBroadCast implements BroadCast { 10 | 11 | private static String channel_prefix="atoms_channel"; 12 | private String channel = null; 13 | 14 | private JGroupsPubSub jGroupsPubSub; 15 | 16 | public void init(AtomsBroadCastBean broadcastBean){ 17 | jGroupsPubSub =new JGroupsPubSub(broadcastBean); 18 | if(StringUtils.isNotEmpty(broadcastBean.getChannel())) { 19 | channel = channel_prefix+"_"+broadcastBean.getChannel(); 20 | }else{ 21 | channel = channel_prefix; 22 | } 23 | try { 24 | jGroupsPubSub.channel.connect(this.channel); 25 | } catch (Exception e) { 26 | e.printStackTrace(); 27 | } 28 | } 29 | 30 | public void broadcast(byte[] message){ 31 | jGroupsPubSub.pub(channel, message); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /atoms/src/test/java/com/lovver/atoms/spring/UserService.java: -------------------------------------------------------------------------------- 1 | package com.lovver.atoms.spring; 2 | 3 | 4 | import org.springframework.cache.annotation.CacheEvict; 5 | import org.springframework.cache.annotation.Cacheable; 6 | 7 | /** 8 | * 业务服务 9 | * 10 | */ 11 | public class UserService { 12 | 13 | @Cacheable(value = "userCache",key="#userName") 14 | // 使用了一个缓存名叫 userCache 15 | public User getUserByName(String userName) { 16 | // 方法内部实现不考虑缓存逻辑,直接实现业务 17 | return getFromDB(userName); 18 | } 19 | 20 | @CacheEvict(value = "userCache", key = "#user.name") 21 | // 清空 accountCache 缓存 22 | public void updateUser(User user) { 23 | updateDB(user); 24 | } 25 | 26 | @CacheEvict(value = "userCache", allEntries = true,beforeInvocation=true) 27 | // 清空 accountCache 缓存 28 | public void reload() { 29 | } 30 | 31 | private User getFromDB(String userName) { 32 | System.out.println("查询数据库..." + userName); 33 | return new User(userName); 34 | } 35 | 36 | private void updateDB(User user) { 37 | System.out.println("更新数据库数据..." + user.getName()); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /atoms-springboot/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.lovver.atoms 6 | atoms-parent 7 | 1.0.18-SNAPSHOT 8 | 9 | atoms-springboot 10 | 1.0.18-SNAPSHOT 11 | pom 12 | 13 | 14 | 15 | maven-compiler-plugin 16 | 17 | 1.7 18 | 1.7 19 | 20 | 21 | 22 | 23 | 24 | 25 | atoms-spring-boot-starter 26 | atoms-spring-boot-autoconfigue 27 | atoms-spring-boot-test 28 | 29 | 30 | -------------------------------------------------------------------------------- /atoms-cache/atoms-cache-api/src/main/java/com/lovver/atoms/cache/CacheEventListener.java: -------------------------------------------------------------------------------- 1 | package com.lovver.atoms.cache; 2 | 3 | import com.lovver.atoms.common.annotation.Extension; 4 | import com.lovver.atoms.common.exception.CacheException; 5 | 6 | /** 7 | * 侦听缓存中的某个记录超时 8 | * @author jobell 9 | */ 10 | @Extension("spi") 11 | public interface CacheEventListener { 12 | 13 | public void init(int level); 14 | 15 | /** 16 | * 当缓存中的某个对象超时被清除的时候触发 17 | * @param region: Cache region name 18 | * @param key: cache key 19 | */ 20 | public void notifyElementExpired(String region, Object key) ; 21 | 22 | public void notifyElementRemoved(String region, Object key) throws CacheException; 23 | 24 | public void notifyElementPut(String region, Object key,Object value) throws CacheException; 25 | public void notifyElementPut(String region, Object key,Object value,int expiretime) throws CacheException; 26 | 27 | public void notifyElementUpdated(String region, Object key,Object value) throws CacheException; 28 | 29 | public void notifyElementEvicted(String region, Object key); 30 | 31 | public void notifyRemoveAll(String region); 32 | 33 | } 34 | -------------------------------------------------------------------------------- /atoms-serializer/atoms-serializer-api/src/main/java/com/lovver/atoms/serializer/SerializerFactory.java: -------------------------------------------------------------------------------- 1 | package com.lovver.atoms.serializer; 2 | 3 | import com.lovver.atoms.common.extension.ExtensionLoader; 4 | import com.lovver.atoms.common.utils.StringUtils; 5 | import com.lovver.atoms.config.AtomsSerializerBean; 6 | 7 | public class SerializerFactory { 8 | private static ExtensionLoader exloader = ExtensionLoader 9 | .getExtensionLoader(Serializer.class); 10 | 11 | public static Serializer getSerializer(AtomsSerializerBean atomSerializerBean) { 12 | String type = atomSerializerBean.getType(); 13 | if (StringUtils.isEmpty(type)) { 14 | type = "fst"; 15 | } 16 | return getSerializer(type); 17 | } 18 | 19 | public static Serializer getSerializer(String type) { 20 | Serializer serializer = null; 21 | try { 22 | serializer = exloader.getExtension(type); 23 | } catch (InstantiationException e) { 24 | e.printStackTrace(); 25 | } catch (IllegalAccessException e) { 26 | e.printStackTrace(); 27 | } 28 | return serializer; 29 | } 30 | } -------------------------------------------------------------------------------- /atoms-broadcast/atoms-broadcast-redis/src/test/java/com/sample/redis/pubsub/persistent/PPubSubTestMain.java: -------------------------------------------------------------------------------- 1 | package com.sample.redis.pubsub.persistent; 2 | 3 | import org.apache.commons.lang.RandomStringUtils; 4 | 5 | import com.sample.redis.Constants; 6 | 7 | public class PPubSubTestMain { 8 | 9 | /** 10 | * @param args 11 | */ 12 | public static void main(String[] args) throws Exception{ 13 | PPubClient pubClient = new PPubClient(Constants.host, Constants.port); 14 | final String channel = "pubsub-channel-p"; 15 | final PSubClient subClient = new PSubClient(Constants.host, Constants.port,"subClient-1"); 16 | Thread subThread = new Thread(new Runnable() { 17 | 18 | @Override 19 | public void run() { 20 | System.out.println("----------subscribe operation begin-------"); 21 | //在API级别,此处为轮询操作,直到unsubscribe调用,才会返回 22 | subClient.sub(channel); 23 | System.out.println("----------subscribe operation end-------"); 24 | 25 | } 26 | }); 27 | subThread.setDaemon(true); 28 | subThread.start(); 29 | int i = 0; 30 | while(i < 2){ 31 | String message = RandomStringUtils.random(6, true, true);//apache-commons 32 | pubClient.pub(channel, message); 33 | i++; 34 | Thread.sleep(1000); 35 | } 36 | subClient.unsubscribe(channel); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /atoms/src/test/resources/redis-atoms.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 21 | 22 | -------------------------------------------------------------------------------- /atoms-springboot/atoms-spring-boot-autoconfigue/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.lovver.atoms 6 | atoms-springboot 7 | 1.0.18-SNAPSHOT 8 | 9 | atoms-spring-boot-autoconfigue 10 | jar 11 | 12 | 13 | org.springframework.boot 14 | spring-boot-autoconfigure 15 | ${spring-boot.version} 16 | 17 | 18 | org.springframework.boot 19 | spring-boot-configuration-processor 20 | ${spring-boot.version} 21 | 22 | 23 | com.lovver.atoms 24 | atoms 25 | 1.0.18-SNAPSHOT 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /atoms-broadcast/atoms-broadcast-redis/src/main/java/com/lovver/atoms/broadcast/redis/support/SafeEncoder.java: -------------------------------------------------------------------------------- 1 | package com.lovver.atoms.broadcast.redis.support; 2 | 3 | import redis.clients.jedis.Protocol; 4 | import redis.clients.jedis.exceptions.JedisDataException; 5 | import redis.clients.jedis.exceptions.JedisException; 6 | 7 | import java.io.UnsupportedEncodingException; 8 | 9 | public class SafeEncoder { 10 | public static byte[][] encodeMany(final String... strs) { 11 | byte[][] many = new byte[strs.length][]; 12 | for (int i = 0; i < strs.length; i++) { 13 | many[i] = encode(strs[i]); 14 | } 15 | return many; 16 | } 17 | 18 | public static byte[] encode(final String str) { 19 | try { 20 | if (str == null) { 21 | throw new JedisDataException("value sent to redis cannot be null"); 22 | } 23 | return str.getBytes(Protocol.CHARSET); 24 | } catch (UnsupportedEncodingException e) { 25 | throw new JedisException(e); 26 | } 27 | } 28 | 29 | public static String encode(final byte[] data) { 30 | try { 31 | return new String(data, Protocol.CHARSET); 32 | } catch (UnsupportedEncodingException e) { 33 | throw new JedisException(e); 34 | } 35 | } 36 | } -------------------------------------------------------------------------------- /atoms-broadcast/atoms-broadcast-redis/src/main/java/com/lovver/atoms/broadcast/redis/RedisBroadCast.java: -------------------------------------------------------------------------------- 1 | package com.lovver.atoms.broadcast.redis; 2 | 3 | import com.lovver.atoms.broadcast.BroadCast; 4 | import com.lovver.atoms.common.annotation.SPI; 5 | import com.lovver.atoms.config.AtomsBroadCastBean; 6 | import org.apache.commons.lang.StringUtils; 7 | 8 | @SPI("redis") 9 | public class RedisBroadCast implements BroadCast{ 10 | 11 | private static String channel_prefix="atoms_channel"; 12 | private String channel = null; 13 | 14 | // private RedisPubSub redisSub; 15 | private RedisPubSub redisPubSub; 16 | 17 | private Thread thread_subscribe; 18 | 19 | public void init(AtomsBroadCastBean broadcastBean){ 20 | redisPubSub=new RedisPubSub(broadcastBean); 21 | // redisPub=new RedisPubSub(broadcastBean); 22 | if(StringUtils.isNotEmpty(broadcastBean.getChannel())) { 23 | channel = channel_prefix+"_"+broadcastBean.getChannel(); 24 | }else{ 25 | channel = channel_prefix; 26 | } 27 | thread_subscribe = new Thread(new Runnable() { 28 | @Override 29 | public void run() { 30 | redisPubSub.sub(redisPubSub, channel); 31 | } 32 | }); 33 | thread_subscribe.start(); 34 | } 35 | 36 | public void broadcast(byte[] message){ 37 | redisPubSub.pub(channel, message); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /atoms-springboot/atoms-spring-boot-test/src/main/resources/application.yaml: -------------------------------------------------------------------------------- 1 | atoms: 2 | application: aa 3 | broadcast: 4 | type: redis 5 | channel: testch 6 | broadcastConfig: 7 | host: 192.168.1.85 8 | port: 6379 9 | usePool: true 10 | timeout: 0 11 | lstBroadset: 12 | - region: hello 13 | key: ddd 14 | - region: jregion 15 | key: jkey 16 | serializer: 17 | type: fst 18 | cache: 19 | - level: 1 20 | type: ehcache 21 | cacheTTL: 22 | lstTTL: 23 | - name: hello 24 | value: 100000000 25 | broadset: false 26 | cacheConfig: 27 | configFile: ehcache.xml 28 | 29 | 30 | - level: 2 31 | type: redis 32 | cacheConfig: 33 | host: 192.168.1.85 34 | port: 6379 35 | timeout: 2000 36 | database: 15 37 | namespace: atoms 38 | maxTotal: -1 39 | maxIdle: 2000 40 | maxWaitMillis: 100 41 | minEvictableIdleTimeMillis: 864000000 42 | minIdle: 100 43 | numTestsPerEvictionRun: 10 44 | lifo: false 45 | softMinEvictableIdleTimeMillis: 10 46 | testOnBorrow: true 47 | testOnReturn: false 48 | testWhileIdle: false 49 | timeBetweenEvictionRunsMillis: 300000 50 | blockWhenExhausted: true 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /atoms/src/test/java/com/lovver/atoms/SerializerFactoryTest.java: -------------------------------------------------------------------------------- 1 | package com.lovver.atoms; 2 | 3 | import com.lovver.atoms.context.AtomsContext; 4 | import com.lovver.atoms.serializer.Serializer; 5 | import com.lovver.atoms.serializer.SerializerFactory; 6 | import org.junit.Test; 7 | import org.junit.runner.RunWith; 8 | import org.springframework.test.context.ContextConfiguration; 9 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 10 | import org.springframework.test.context.transaction.TransactionConfiguration; 11 | 12 | import java.io.IOException; 13 | import java.util.ArrayList; 14 | import java.util.Arrays; 15 | import java.util.List; 16 | 17 | /** 18 | * 对象序列化工具包 19 | * 20 | * @author jobell 21 | */ 22 | public class SerializerFactoryTest { 23 | 24 | @Test 25 | public void main() throws IOException { 26 | 27 | Serializer g_ser= SerializerFactory.getSerializer("fst"); 28 | 29 | final List obj = new ArrayList(); 30 | obj.addAll(Arrays.asList("哈啊啊", "农夫山泉", "康师傅", "啊百特C", "多大的", "反反复复")); 31 | 32 | byte[] bits = g_ser.serialize(obj); 33 | for (byte b : bits) { 34 | System.out.print(Byte.toString(b) + " "); 35 | } 36 | System.out.println(); 37 | System.out.println(bits.length); 38 | System.out.println(g_ser.deserialize(bits)); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /atoms-broadcast/atoms-broadcast-redis/src/test/java/com/sample/redis/pubsub/persistent/PPubClient.java: -------------------------------------------------------------------------------- 1 | package com.sample.redis.pubsub.persistent; 2 | 3 | import java.util.Set; 4 | 5 | import redis.clients.jedis.Jedis; 6 | 7 | import com.sample.redis.Constants; 8 | 9 | public class PPubClient { 10 | 11 | private Jedis jedis;// 12 | public PPubClient(String host,int port){ 13 | jedis = new Jedis(host,port); 14 | } 15 | 16 | /** 17 | * 发布的每条消息,都需要在“订阅者消息队列”中持久 18 | * @param message 19 | */ 20 | private void put(String message){ 21 | //期望这个集合不要太大 22 | Set subClients = jedis.smembers(Constants.SUBSCRIBE_CENTER); 23 | for(String clientKey : subClients){ 24 | jedis.rpush(clientKey, message); 25 | } 26 | } 27 | 28 | public void pub(String channel,String message){ 29 | //每个消息,都有具有一个全局唯一的id 30 | //txid为了防止订阅端在数据处理时“乱序”,这就要求订阅者需要解析message 31 | Long txid = jedis.incr(Constants.MESSAGE_TXID); 32 | String content = txid + "/" + message; 33 | //非事务 34 | this.put(content); 35 | jedis.publish(channel, content);//为每个消息设定id,最终消息格式1000/messageContent 36 | 37 | } 38 | 39 | public void close(String channel){ 40 | jedis.publish(channel, "quit"); 41 | jedis.del(channel);//删除 42 | } 43 | 44 | public void test(){ 45 | jedis.set("pub-block", "15"); 46 | String tmp = jedis.get("pub-block"); 47 | System.out.println("TEST:" + tmp); 48 | } 49 | 50 | 51 | } 52 | -------------------------------------------------------------------------------- /atoms-config/src/main/java/com/lovver/atoms/config/AtomsBean.java: -------------------------------------------------------------------------------- 1 | package com.lovver.atoms.config; 2 | 3 | import java.util.List; 4 | 5 | import com.thoughtworks.xstream.annotations.XStreamAlias; 6 | import com.thoughtworks.xstream.annotations.XStreamAsAttribute; 7 | import com.thoughtworks.xstream.annotations.XStreamImplicit; 8 | 9 | @XStreamAlias("atoms") 10 | public class AtomsBean { 11 | 12 | private AtomsBroadCastBean broadcast; 13 | 14 | private AtomsSerializerBean serializer; 15 | 16 | @XStreamAsAttribute 17 | private String application="atoms"; 18 | 19 | @XStreamImplicit(itemFieldName = "cache") 20 | private List cache; 21 | 22 | public AtomsBroadCastBean getBroadcast() { 23 | return broadcast; 24 | } 25 | 26 | public void setBroadcast(AtomsBroadCastBean broadcast) { 27 | this.broadcast = broadcast; 28 | } 29 | 30 | public AtomsSerializerBean getSerializer() { 31 | return serializer; 32 | } 33 | 34 | public void setSerializer(AtomsSerializerBean serializer) { 35 | this.serializer = serializer; 36 | } 37 | 38 | public List getCache() { 39 | return cache; 40 | } 41 | 42 | public void setCache(List cache) { 43 | this.cache = cache; 44 | } 45 | 46 | public String getApplication() { 47 | return application; 48 | } 49 | 50 | public void setApplication(String application) { 51 | this.application = application; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /atoms-config/src/test/resources/atoms.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 23 | 24 | -------------------------------------------------------------------------------- /atoms/src/test/java/com/lovver/atoms/CommandTest.java: -------------------------------------------------------------------------------- 1 | package com.lovver.atoms; 2 | 3 | import com.lovver.atoms.broadcast.Command; 4 | import org.junit.Test; 5 | import org.junit.runner.RunWith; 6 | import org.springframework.test.context.ContextConfiguration; 7 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 8 | import org.springframework.test.context.transaction.TransactionConfiguration; 9 | 10 | /** 11 | * 命令消息封装 12 | * 格式: 13 | * 第1个字节为命令代码,长度1 [OPT] 14 | * 第2、3个字节为region长度,长度2 [R_LEN] 15 | * 第4、N 为 region 值,长度为 [R_LEN] 16 | * 第N+1、N+2 为 key 长度,长度2 [K_LEN] 17 | * 第N+3、M为 key值,长度为 [K_LEN] 18 | * 19 | * @author winterlau 20 | */ 21 | @RunWith(SpringJUnit4ClassRunner.class) 22 | @ContextConfiguration(value = {"classpath:applicationContext.xml"}) 23 | @TransactionConfiguration(defaultRollback = false) 24 | public class CommandTest { 25 | 26 | @Test 27 | public void runTest() { 28 | 29 | for(int i=0;i<5;i++){ 30 | Command cmd = new Command(Command.OPT_DELETE_KEY, "users", "ld"+i); 31 | byte[] bufs = cmd.toBuffers(); 32 | System.out.print(cmd.getSrc() + ":"); 33 | for(byte b : bufs){ 34 | System.out.printf("[%s]",Integer.toHexString(b)); 35 | } 36 | System.out.println(); 37 | Command cmd2 = Command.parse(bufs); 38 | System.out.printf("%d -> %d:%s:%s:%s:%d(%s)\n", cmd2.getSrc(), cmd2.getOperator(), cmd2.getRegion(), cmd2.getKey(),cmd2.getVal(),cmd2.getExpiretime(), cmd2.isLocalCommand()); 39 | } 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /atoms/src/test/java/com/lovver/atoms/SerializerTest.java: -------------------------------------------------------------------------------- 1 | package com.lovver.atoms; 2 | 3 | import java.io.IOException; 4 | import java.util.ArrayList; 5 | import java.util.Arrays; 6 | import java.util.List; 7 | 8 | 9 | import com.lovver.atoms.context.AtomsContext; 10 | import com.lovver.atoms.serializer.Serializer; 11 | import org.junit.Test; 12 | import org.junit.runner.RunWith; 13 | import org.springframework.test.context.ContextConfiguration; 14 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 15 | import org.springframework.test.context.transaction.TransactionConfiguration; 16 | 17 | /** 18 | * 对象序列化工具包 19 | * 20 | * @author jobell 21 | */ 22 | @RunWith(SpringJUnit4ClassRunner.class) 23 | @ContextConfiguration(value = {"classpath:applicationContext.xml"}) 24 | @TransactionConfiguration(defaultRollback = false) 25 | public class SerializerTest { 26 | 27 | @Test 28 | public void main() throws IOException { 29 | 30 | Serializer g_ser= AtomsContext.getSerializer(); 31 | 32 | final List obj = new ArrayList(); 33 | obj.addAll(Arrays.asList("哈啊啊", "农夫山泉", "康师傅", "啊百特C", "多大的", "反反复复")); 34 | 35 | byte[] bits = g_ser.serialize(obj); 36 | for (byte b : bits) { 37 | System.out.print(Byte.toString(b) + " "); 38 | } 39 | System.out.println(); 40 | System.out.println(bits.length); 41 | System.out.println(g_ser.deserialize(bits)); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /atoms-cache/atoms-cache-api/src/main/java/com/lovver/atoms/cache/CacheProvider.java: -------------------------------------------------------------------------------- 1 | package com.lovver.atoms.cache; 2 | 3 | import com.lovver.atoms.common.annotation.Extension; 4 | import com.lovver.atoms.common.exception.CacheException; 5 | import com.lovver.atoms.config.AtomsCacheBean; 6 | 7 | /** 8 | * Support for pluggable caches. 9 | * @author liudong 10 | */ 11 | @Extension("spi") 12 | public interface CacheProvider { 13 | 14 | /** 15 | * 缓存的标识名称 16 | * @return return cache provider name 17 | */ 18 | public String name(); 19 | 20 | /** 21 | * Configure the cache 22 | * 23 | * @param regionName the name of the cache region 24 | * @param autoCreate autoCreate settings 25 | * @param listener listener for expired elements 26 | * @return return cache instance 27 | * @throws CacheException cache exception 28 | */ 29 | public Cache buildCache(String regionName, boolean autoCreate, CacheEventListener listener) throws CacheException; 30 | 31 | /** 32 | * Callback to perform any necessary initialization of the underlying cache implementation 33 | * during SessionFactory construction. 34 | * 35 | * @param props current configuration settings. 36 | */ 37 | public void start(AtomsCacheBean cacheBean) throws CacheException; 38 | 39 | /** 40 | * Callback to perform any necessary cleanup of the underlying cache implementation 41 | * during SessionFactory.close(). 42 | */ 43 | public void stop(); 44 | 45 | } 46 | -------------------------------------------------------------------------------- /atoms-springboot/atoms-spring-boot-test/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.lovver.atoms 6 | atoms-springboot 7 | 1.0.18-SNAPSHOT 8 | 9 | atoms-spring-boot-test 10 | 11 | 12 | com.lovver.atoms 13 | atoms 14 | 1.0.18-SNAPSHOT 15 | 16 | 17 | 18 | com.lovver.atoms 19 | atoms-spring-boot-starter 20 | 1.0.18-SNAPSHOT 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-web 25 | ${spring-boot.version} 26 | 27 | 28 | logback-classic 29 | ch.qos.logback 30 | 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /atoms-serializer/atoms-serializer-kryo/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.lovver.atoms 6 | atoms-serializer 7 | 1.0.18-SNAPSHOT 8 | 9 | atoms-serializer-kryo 10 | 11 | 12 | 13 | org.apache.maven.plugins 14 | maven-source-plugin 15 | 3.0.1 16 | 17 | true 18 | 19 | 20 | 21 | compile 22 | 23 | jar 24 | 25 | 26 | 27 | 28 | 29 | 30 | maven-compiler-plugin 31 | 32 | 1.7 33 | 1.7 34 | 35 | 36 | 37 | 38 | 39 | 40 | com.lovver.atoms 41 | atoms-serializer-api 42 | 1.0.18-SNAPSHOT 43 | 44 | 45 | com.esotericsoftware 46 | kryo-shaded 47 | 3.0.0 48 | 49 | 50 | -------------------------------------------------------------------------------- /atoms-serializer/atoms-serializer-kryopool/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.lovver.atoms 6 | atoms-serializer 7 | 1.0.18-SNAPSHOT 8 | 9 | atoms-serializer-kryopool 10 | 11 | 12 | 13 | org.apache.maven.plugins 14 | maven-source-plugin 15 | 3.0.1 16 | 17 | true 18 | 19 | 20 | 21 | compile 22 | 23 | jar 24 | 25 | 26 | 27 | 28 | 29 | 30 | maven-compiler-plugin 31 | 32 | 1.7 33 | 1.7 34 | 35 | 36 | 37 | 38 | 39 | 40 | com.lovver.atoms 41 | atoms-serializer-api 42 | 1.0.18-SNAPSHOT 43 | 44 | 45 | com.esotericsoftware 46 | kryo-shaded 47 | 3.0.0 48 | 49 | 50 | -------------------------------------------------------------------------------- /atoms-config/src/main/java/com/lovver/atoms/config/AtomsBroadCastBean.java: -------------------------------------------------------------------------------- 1 | package com.lovver.atoms.config; 2 | 3 | import com.thoughtworks.xstream.annotations.XStreamAlias; 4 | import com.thoughtworks.xstream.annotations.XStreamAsAttribute; 5 | import com.thoughtworks.xstream.annotations.XStreamImplicit; 6 | 7 | import java.util.List; 8 | 9 | @XStreamAlias("broadcast") 10 | public class AtomsBroadCastBean { 11 | 12 | @XStreamAsAttribute 13 | private String type; 14 | 15 | @XStreamAsAttribute 16 | private String channel; 17 | 18 | private AtomsBroadCastConfigBean broadcastConfig; 19 | 20 | @XStreamImplicit(itemFieldName = "broadset") 21 | private List lstBroadset; 22 | // 23 | // @XStreamAsAttribute 24 | // private String host; 25 | // 26 | // @XStreamAsAttribute 27 | // private String port; 28 | 29 | public String getType() { 30 | return type; 31 | } 32 | 33 | public void setType(String type) { 34 | this.type = type; 35 | } 36 | 37 | public AtomsBroadCastConfigBean getBroadcastConfig() { 38 | return broadcastConfig; 39 | } 40 | 41 | public void setBroadcastConfig(AtomsBroadCastConfigBean broadcastConfig) { 42 | this.broadcastConfig = broadcastConfig; 43 | } 44 | 45 | public String getChannel() { 46 | return channel; 47 | } 48 | 49 | public void setChannel(String channel) { 50 | this.channel = channel; 51 | } 52 | 53 | public List getLstBroadset() { 54 | return lstBroadset; 55 | } 56 | 57 | public void setLstBroadset(List lstBroadset) { 58 | this.lstBroadset = lstBroadset; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /atoms-serializer/atoms-serializer-kryo/src/main/java/com/lovver/atoms/serializer/kryo/KryoSerializer.java: -------------------------------------------------------------------------------- 1 | package com.lovver.atoms.serializer.kryo; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.ByteArrayOutputStream; 5 | import java.io.IOException; 6 | 7 | import com.esotericsoftware.kryo.Kryo; 8 | import com.esotericsoftware.kryo.io.Input; 9 | import com.esotericsoftware.kryo.io.Output; 10 | import com.lovver.atoms.common.annotation.SPI; 11 | import com.lovver.atoms.serializer.Serializer; 12 | 13 | /** 14 | * 使用 Kryo 实现序列化 15 | * @author jobell 16 | */ 17 | @SPI("kryo") 18 | public class KryoSerializer implements Serializer { 19 | 20 | private final static Kryo kryo = new Kryo(); 21 | 22 | 23 | @Override 24 | public String name() { 25 | return "kryo"; 26 | } 27 | 28 | @Override 29 | public byte[] serialize(Object obj) throws IOException { 30 | Output output = null; 31 | try { 32 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); 33 | output = new Output(baos); 34 | kryo.writeClassAndObject(output, obj); 35 | output.flush(); 36 | return baos.toByteArray(); 37 | }finally{ 38 | if(output != null) 39 | output.close(); 40 | } 41 | } 42 | 43 | @Override 44 | public Object deserialize(byte[] bits) throws IOException { 45 | if(bits == null || bits.length == 0) 46 | return null; 47 | Input ois = null; 48 | try { 49 | ByteArrayInputStream bais = new ByteArrayInputStream(bits); 50 | ois = new Input(bais); 51 | return kryo.readClassAndObject(ois); 52 | } finally { 53 | if(ois != null) 54 | ois.close(); 55 | } 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /atoms-spring/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | 4 | com.lovver.atoms 5 | atoms-parent 6 | 1.0.18-SNAPSHOT 7 | 8 | atoms-spring 9 | 10 | 11 | 12 | org.apache.maven.plugins 13 | maven-source-plugin 14 | 3.0.1 15 | 16 | true 17 | 18 | 19 | 20 | compile 21 | 22 | jar 23 | 24 | 25 | 26 | 27 | 28 | 29 | maven-compiler-plugin 30 | 31 | 1.7 32 | 1.7 33 | 34 | 35 | 36 | 37 | 38 | 39 | com.lovver.atoms 40 | atoms-core 41 | 1.0.18-SNAPSHOT 42 | 43 | 44 | org.springframework 45 | spring-context 46 | ${spring.version} 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /atoms/src/test/java/com/lovver/atoms/spring/SpringCacheTest.java: -------------------------------------------------------------------------------- 1 | package com.lovver.atoms.spring; 2 | 3 | 4 | import org.springframework.context.ApplicationContext; 5 | import org.springframework.context.support.ClassPathXmlApplicationContext; 6 | 7 | /** 8 | * Created by Administrator on 2017/3/20. 9 | */ 10 | public class SpringCacheTest { 11 | 12 | public static void main(String[] args) throws InterruptedException { 13 | ApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"classpath:applicationContext-springcache.xml"}); 14 | 15 | UserService userService = context.getBean(UserService.class); 16 | // 第一次查询,应该走数据库 17 | System.out.print("第一次查询..."); 18 | userService.getUserByName("hello"); 19 | // 第二次查询,应该不查数据库,直接返回缓存的值 20 | System.out.println("第二次查询..."); 21 | userService.getUserByName("hello"); 22 | System.out.println(); 23 | System.out.println("=============="); 24 | 25 | // 更新某个记录的缓存,首先构造两个用户记录,然后记录到缓存中 26 | User user1 = userService.getUserByName("user1"); 27 | // 开始更新其中一个 28 | user1.setId(1000); 29 | userService.updateUser(user1); 30 | // 因为被更新了,所以会查询数据库 31 | userService.getUserByName("user1"); 32 | // 再次查询,应该走缓存 33 | userService.getUserByName("user1"); 34 | // 更新所有缓存 35 | userService.reload(); 36 | System.out.println("清楚所有缓存"); 37 | // 查询数据库 38 | userService.getUserByName("user1"); 39 | userService.getUserByName("user2"); 40 | // 查询缓存 41 | userService.getUserByName("user1"); 42 | userService.getUserByName("user2"); 43 | 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /atoms-serializer/atoms-serializer-java/src/main/java/com/lovver/atoms/serializer/java/JavaSerializer.java: -------------------------------------------------------------------------------- 1 | package com.lovver.atoms.serializer.java; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.ByteArrayOutputStream; 5 | import java.io.IOException; 6 | import java.io.ObjectInputStream; 7 | import java.io.ObjectOutputStream; 8 | 9 | import com.lovver.atoms.common.annotation.SPI; 10 | import com.lovver.atoms.common.exception.CacheException; 11 | import com.lovver.atoms.serializer.Serializer; 12 | 13 | /** 14 | * 标准的 Java 序列化 15 | * @author jobell 16 | */ 17 | @SPI("java") 18 | public class JavaSerializer implements Serializer { 19 | 20 | @Override 21 | public String name() { 22 | return "java"; 23 | } 24 | 25 | @Override 26 | public byte[] serialize(Object obj) throws IOException { 27 | ObjectOutputStream oos = null; 28 | try { 29 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); 30 | oos = new ObjectOutputStream(baos); 31 | oos.writeObject(obj); 32 | return baos.toByteArray(); 33 | } finally { 34 | if(oos != null) 35 | try { 36 | oos.close(); 37 | } catch (IOException e) {} 38 | } 39 | } 40 | 41 | @Override 42 | public Object deserialize(byte[] bits) throws IOException { 43 | if(bits == null || bits.length == 0) 44 | return null; 45 | ObjectInputStream ois = null; 46 | try { 47 | ByteArrayInputStream bais = new ByteArrayInputStream(bits); 48 | ois = new ObjectInputStream(bais); 49 | return ois.readObject(); 50 | } catch (ClassNotFoundException e) { 51 | throw new CacheException(e); 52 | } finally { 53 | if(ois != null) 54 | try { 55 | ois.close(); 56 | } catch (IOException e) {} 57 | } 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /atoms-serializer/atoms-serializer-fst/src/main/java/com/lovver/atoms/serializer/fst/FSTSerializer.java: -------------------------------------------------------------------------------- 1 | package com.lovver.atoms.serializer.fst; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.ByteArrayOutputStream; 5 | import java.io.IOException; 6 | 7 | import org.nustaq.serialization.FSTObjectInput; 8 | import org.nustaq.serialization.FSTObjectOutput; 9 | 10 | import com.lovver.atoms.common.annotation.SPI; 11 | import com.lovver.atoms.common.exception.CacheException; 12 | import com.lovver.atoms.serializer.Serializer; 13 | 14 | /** 15 | * 使用 FST 实现序列化 16 | * @author jobell 17 | */ 18 | @SPI("fst") 19 | public class FSTSerializer implements Serializer { 20 | 21 | @Override 22 | public String name() { 23 | return "fst"; 24 | } 25 | 26 | @Override 27 | public byte[] serialize(Object obj) throws IOException { 28 | ByteArrayOutputStream out = null; 29 | FSTObjectOutput fout = null; 30 | try { 31 | out = new ByteArrayOutputStream(); 32 | fout = new FSTObjectOutput(out); 33 | fout.writeObject(obj); 34 | fout.flush(); 35 | return out.toByteArray(); 36 | } finally { 37 | if(fout != null) 38 | try { 39 | fout.close(); 40 | } catch (IOException e) {} 41 | } 42 | } 43 | 44 | @Override 45 | public Object deserialize(byte[] bytes) throws IOException { 46 | if(bytes == null || bytes.length == 0) 47 | return null; 48 | FSTObjectInput in = null; 49 | try { 50 | in = new FSTObjectInput(new ByteArrayInputStream(bytes)); 51 | return in.readObject(); 52 | } catch (ClassNotFoundException e) { 53 | throw new CacheException(e); 54 | } finally { 55 | if(in != null) 56 | try { 57 | in.close(); 58 | } catch (IOException e) {} 59 | } 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /atoms/src/test/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | # ------------------------------------------------------------------- 2 | # ONLINE EDU LOG4J.PROPERTIES 3 | # ------------------------------------------------------------------- 4 | log4j.rootLogger=WARN, stdout 5 | # ,A2,MAIL 6 | 7 | log4j.appender.stdout=org.apache.log4j.ConsoleAppender 8 | log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 9 | log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %7p [%c] %m%n 10 | log4j.logger.noModule=FATAL 11 | 12 | #log4j.appender.A2=org.apache.log4j.DailyRollingFileAppender 13 | #log4j.appender.A2.file=../logs/admin.log 14 | #log4j.appender.A2.DatePattern='.'yyyy-MM-dd 15 | #log4j.appender.A2.layout=org.apache.log4j.PatternLayout 16 | #log4j.appender.A2.layout.ConversionPattern= %d{yyyy-MM-dd HH:mm:ss,SSS} %7p [%c] %m%n 17 | 18 | ## MAIL 19 | #log4j.appender.MAIL=org.apache.log4j.net.SMTPAppender 20 | #log4j.appender.MAIL.Threshold=ERROR 21 | #log4j.appender.MAIL.BufferSize=512 22 | #log4j.appender.MAIL.SMTPHost=smtp.exmail.qq.com 23 | #log4j.appender.MAIL.Subject=Log4J ErrorMessage 24 | #log4j.appender.MAIL.SMTPUsername=error@tianxiameng.com 25 | #log4j.appender.MAIL.SMTPPassword=aaaa1111 26 | #log4j.appender.MAIL.From=error@tianxiameng.com 27 | #log4j.appender.MAIL.To=jiangbiao@tianxiameng.com,sushujuan@tianxiameng.com 28 | #log4j.appender.MAIL.layout=org.apache.log4j.PatternLayout 29 | #log4j.appender.MAIL.layout.ConversionPattern=[ErrorMessage ] %d - %c -%-4r [%t] %-5p %c %x - %m%n 30 | 31 | # OpenSymphony Stuff 32 | #log4j.logger.com.opensymphony=INFO 33 | #log4j.logger.org.apache.struts2=INFO 34 | # Spring Stuff 35 | #log4j.logger.org.springframework=INFO 36 | # Oledu Stuff 37 | log4j.logger.com.lovver=DEBUG 38 | 39 | -------------------------------------------------------------------------------- /atoms-broadcast/atoms-broadcast-redis/src/test/java/com/sample/redis/pubsub/simple/PrintListener.java: -------------------------------------------------------------------------------- 1 | package com.sample.redis.pubsub.simple; 2 | 3 | import java.util.Date; 4 | 5 | import org.apache.commons.lang.time.DateFormatUtils; 6 | 7 | import redis.clients.jedis.JedisPubSub; 8 | 9 | public class PrintListener extends JedisPubSub{ 10 | 11 | @Override 12 | public void onMessage(String channel, String message) { 13 | String time = DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"); 14 | System.out.println("message receive:" + message + ",channel:" + channel + "..." + time); 15 | //此处我们可以取消订阅 16 | if(message.equalsIgnoreCase("quit")){ 17 | this.unsubscribe(channel); 18 | } 19 | } 20 | 21 | @Override 22 | public void onPMessage(String pattern, String channel, String message) { 23 | System.out.println("message receive:" + message + ",pattern channel:" + channel); 24 | 25 | } 26 | 27 | @Override 28 | public void onSubscribe(String channel, int subscribedChannels) { 29 | System.out.println("subscribe:" + channel + ";total channels : " + subscribedChannels); 30 | 31 | } 32 | 33 | @Override 34 | public void onUnsubscribe(String channel, int subscribedChannels) { 35 | System.out.println("unsubscribe:" + channel + ";total channels : " + subscribedChannels); 36 | 37 | } 38 | 39 | @Override 40 | public void onPUnsubscribe(String pattern, int subscribedChannels) { 41 | System.out.println("unsubscribe pattern:" + pattern + ";total channels : " + subscribedChannels); 42 | 43 | } 44 | 45 | @Override 46 | public void onPSubscribe(String pattern, int subscribedChannels) { 47 | System.out.println("subscribe pattern:" + pattern + ";total channels : " + subscribedChannels); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /atoms/src/test/resources/jgroup-atoms.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /atoms-cache/atoms-cache-redis/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | 4 | com.lovver.atoms 5 | atoms-cache 6 | 1.0.18-SNAPSHOT 7 | 8 | atoms-cache-redis 9 | 10 | 11 | 12 | org.apache.maven.plugins 13 | maven-source-plugin 14 | 3.0.1 15 | 16 | true 17 | 18 | 19 | 20 | compile 21 | 22 | jar 23 | 24 | 25 | 26 | 27 | 28 | 29 | maven-compiler-plugin 30 | 31 | 1.7 32 | 1.7 33 | 34 | 35 | 36 | 37 | 38 | 39 | com.lovver.atoms 40 | atoms-cache-api 41 | 1.0.18-SNAPSHOT 42 | 43 | 44 | redis.clients 45 | jedis 46 | 2.8.0 47 | 48 | 49 | com.lovver.atoms 50 | atoms-context 51 | 1.0.18-SNAPSHOT 52 | 53 | 54 | -------------------------------------------------------------------------------- /atoms-serializer/atoms-serializer-java/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | 4 | com.lovver.atoms 5 | atoms-serializer 6 | 1.0.18-SNAPSHOT 7 | 8 | atoms-serializer-java 9 | 10 | 11 | 12 | org.apache.maven.plugins 13 | maven-source-plugin 14 | 3.0.1 15 | 16 | true 17 | 18 | 19 | 20 | compile 21 | 22 | jar 23 | 24 | 25 | 26 | 27 | 28 | 29 | maven-compiler-plugin 30 | 31 | 1.7 32 | 1.7 33 | 34 | 35 | 36 | 37 | 38 | 39 | com.lovver.atoms 40 | atoms-serializer-api 41 | 1.0.18-SNAPSHOT 42 | 43 | 44 | -------------------------------------------------------------------------------- /atoms-springboot/atoms-spring-boot-test/src/main/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | # ------------------------------------------------------------------- 2 | # ONLINE EDU LOG4J.PROPERTIES 3 | # ------------------------------------------------------------------- 4 | log4j.rootLogger=WARN, stdout 5 | # ,A2,MAIL 6 | 7 | log4j.appender.stdout=org.apache.log4j.ConsoleAppender 8 | log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 9 | log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %7p [%c] %m%n 10 | log4j.logger.noModule=FATAL 11 | 12 | #log4j.appender.A2=org.apache.log4j.DailyRollingFileAppender 13 | #log4j.appender.A2.file=../logs/admin.log 14 | #log4j.appender.A2.DatePattern='.'yyyy-MM-dd 15 | #log4j.appender.A2.layout=org.apache.log4j.PatternLayout 16 | #log4j.appender.A2.layout.ConversionPattern= %d{yyyy-MM-dd HH:mm:ss,SSS} %7p [%c] %m%n 17 | 18 | ## MAIL 19 | #log4j.appender.MAIL=org.apache.log4j.net.SMTPAppender 20 | #log4j.appender.MAIL.Threshold=ERROR 21 | #log4j.appender.MAIL.BufferSize=512 22 | #log4j.appender.MAIL.SMTPHost=smtp.exmail.qq.com 23 | #log4j.appender.MAIL.Subject=Log4J ErrorMessage 24 | #log4j.appender.MAIL.SMTPUsername=error@tianxiameng.com 25 | #log4j.appender.MAIL.SMTPPassword=aaaa1111 26 | #log4j.appender.MAIL.From=error@tianxiameng.com 27 | #log4j.appender.MAIL.To=jiangbiao@tianxiameng.com,sushujuan@tianxiameng.com 28 | #log4j.appender.MAIL.layout=org.apache.log4j.PatternLayout 29 | #log4j.appender.MAIL.layout.ConversionPattern=[ErrorMessage ] %d - %c -%-4r [%t] %-5p %c %x - %m%n 30 | 31 | # OpenSymphony Stuff 32 | #log4j.logger.com.opensymphony=INFO 33 | #log4j.logger.org.apache.struts2=INFO 34 | # Spring Stuff 35 | #log4j.logger.org.springframework=INFO 36 | # Oledu Stuff 37 | log4j.logger.com.lovver=DEBUG 38 | 39 | -------------------------------------------------------------------------------- /atoms-cache/atoms-cache-ehcache/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | 4 | com.lovver.atoms 5 | atoms-cache 6 | 1.0.18-SNAPSHOT 7 | 8 | atoms-cache-ehcache 9 | 10 | 11 | 12 | org.apache.maven.plugins 13 | maven-source-plugin 14 | 3.0.1 15 | 16 | true 17 | 18 | 19 | 20 | compile 21 | 22 | jar 23 | 24 | 25 | 26 | 27 | 28 | 29 | maven-compiler-plugin 30 | 31 | 1.7 32 | 1.7 33 | 34 | 35 | 36 | 37 | 38 | 39 | com.lovver.atoms 40 | atoms-cache-api 41 | 1.0.18-SNAPSHOT 42 | 43 | 44 | net.sf.ehcache 45 | ehcache-core 46 | 2.6.11 47 | 48 | 49 | com.lovver.atoms 50 | atoms-context 51 | 1.0.18-SNAPSHOT 52 | 53 | 54 | -------------------------------------------------------------------------------- /atoms-broadcast/atoms-broadcast-api/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | 4 | com.lovver.atoms 5 | atoms-broadcast 6 | 1.0.18-SNAPSHOT 7 | 8 | atoms-broadcast-api 9 | 10 | 11 | 12 | org.apache.maven.plugins 13 | maven-source-plugin 14 | 3.0.1 15 | 16 | true 17 | 18 | 19 | 20 | compile 21 | 22 | jar 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | com.lovver.atoms 32 | atoms-common 33 | 1.0.18-SNAPSHOT 34 | 35 | 36 | com.lovver.atoms 37 | atoms-config 38 | 1.0.18-SNAPSHOT 39 | 40 | 41 | com.lovver.atoms 42 | atoms-serializer-api 43 | 1.0.18-SNAPSHOT 44 | 45 | 46 | -------------------------------------------------------------------------------- /atoms-broadcast/atoms-broadcast-redis/src/test/java/com/sample/redis/pubsub/simple/PubSubTestMain.java: -------------------------------------------------------------------------------- 1 | package com.sample.redis.pubsub.simple; 2 | 3 | import org.apache.commons.lang.RandomStringUtils; 4 | 5 | import redis.clients.jedis.JedisPubSub; 6 | 7 | import com.sample.redis.Constants; 8 | 9 | public class PubSubTestMain { 10 | 11 | /** 12 | * @param args 13 | */ 14 | public static void main(String[] args) throws Exception{ 15 | PubClient pubClient = new PubClient(Constants.host, Constants.port); 16 | final String channel = "pubsub-channel"; 17 | pubClient.pub(channel, "before1"); 18 | // pubClient.pub(channel, "before2"); 19 | // Thread.sleep(2000); 20 | //消息订阅着非常特殊,需要独占链接,因此我们需要为它创建新的链接; 21 | //此外,jedis客户端的实现也保证了“链接独占”的特性,sub方法将一直阻塞, 22 | //直到调用listener.unsubscribe方法 23 | Thread subThread = new Thread(new Runnable() { 24 | @Override 25 | public void run() { 26 | try{ 27 | SubClient subClient = new SubClient(Constants.host, Constants.port); 28 | System.out.println("----------subscribe operation begin-------"); 29 | JedisPubSub listener = new PrintListener(); 30 | //在API级别,此处为轮询操作,直到unsubscribe调用,才会返回 31 | subClient.sub(listener, channel); 32 | System.out.println("----------subscribe operation end-------"); 33 | }catch(Exception e){ 34 | e.printStackTrace(); 35 | } 36 | 37 | } 38 | }); 39 | subThread.start(); 40 | int i=0; 41 | while(i < 10){ 42 | String message = "jobell";//RandomStringUtils.random(6, true, true);//apache-commons 43 | pubClient.pub(channel, message+i); 44 | i++; 45 | Thread.sleep(1000); 46 | } 47 | //被动关闭指示,如果通道中,消息发布者确定通道需要关闭,那么就发送一个“quit” 48 | //那么在listener.onMessage()中接收到“quit”时,其他订阅client将执行“unsubscribe”操作。 49 | pubClient.close(channel); 50 | //此外,你还可以这样取消订阅 51 | //listener.unsubscribe(channel); 52 | subThread.interrupt(); 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | 基于j2cache的理念,重新设计开发的一套分布式缓存。。 2 | 3 | github地址:[atoms](https://github.com/zhitongjob/atoms) 4 | ## Who's using atoms? 5 | ![职通网](http://www.zhitongjob.com/images/logo.png) [职通网] (http://www.zhitongjob.com) 6 | 7 | 配置文件 8 | ``` 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 26 | 27 | 28 | 29 | 30 | 31 | ``` 32 | 33 | 使用代码: 34 | ``` java 35 | CacheChannel cc=CacheChannel.getInstance(); 36 | cc.set("jobell", "hello", "nihaoya"); 37 | cc.evict("jobell", "hello"); 38 | while(true){ 39 | Object value=cc.get("jobell", "hello"); 40 | if(value==null){ 41 | System.out.println("==============="+value); 42 | }else{ 43 | System.out.println("==============="+value); 44 | } 45 | } 46 | ``` 47 | 48 | Spring配置参考atoms-config/src/test/resources/applicationContext.xml 49 | -------------------------------------------------------------------------------- /atoms-cache/atoms-cache-api/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | 4 | com.lovver.atoms 5 | atoms-cache 6 | 1.0.18-SNAPSHOT 7 | 8 | atoms-cache-api 9 | 10 | 11 | 12 | org.apache.maven.plugins 13 | maven-source-plugin 14 | 3.0.1 15 | 16 | true 17 | 18 | 19 | 20 | compile 21 | 22 | jar 23 | 24 | 25 | 26 | 27 | 28 | 29 | maven-compiler-plugin 30 | 31 | 1.7 32 | 1.7 33 | 34 | 35 | 36 | 37 | 38 | 39 | com.lovver.atoms 40 | atoms-config 41 | 1.0.18-SNAPSHOT 42 | 43 | 44 | com.lovver.atoms 45 | atoms-serializer-api 46 | 1.0.18-SNAPSHOT 47 | 48 | 49 | -------------------------------------------------------------------------------- /atoms-serializer/atoms-serializer-fst/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | 4 | com.lovver.atoms 5 | atoms-serializer 6 | 1.0.18-SNAPSHOT 7 | 8 | atoms-serializer-fst 9 | 10 | 11 | 12 | org.apache.maven.plugins 13 | maven-source-plugin 14 | 3.0.1 15 | 16 | true 17 | 18 | 19 | 20 | compile 21 | 22 | jar 23 | 24 | 25 | 26 | 27 | 28 | 29 | maven-compiler-plugin 30 | 31 | 1.7 32 | 1.7 33 | 34 | 35 | 36 | 37 | 38 | 39 | com.lovver.atoms 40 | atoms-serializer-api 41 | 1.0.18-SNAPSHOT 42 | 43 | 44 | de.ruedigermoeller 45 | fst 46 | 2.45 47 | 48 | 49 | -------------------------------------------------------------------------------- /atoms-springboot/atoms-spring-boot-autoconfigue/src/main/java/com/lovver/atoms/springboot/autoconfigue/AtomsAutoConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.lovver.atoms.springboot.autoconfigue; 2 | 3 | import com.lovver.atoms.config.AtomsBean; 4 | import com.lovver.atoms.config.AtomsConfig; 5 | import com.lovver.atoms.config.AtomsSpringConfig; 6 | import com.lovver.atoms.context.AtomsContext; 7 | import com.lovver.atoms.core.CacheChannel; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.boot.autoconfigure.AutoConfigureAfter; 10 | import org.springframework.boot.autoconfigure.AutoConfigureOrder; 11 | import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; 12 | import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; 13 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 14 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; 15 | import org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration; 16 | import org.springframework.boot.context.properties.EnableConfigurationProperties; 17 | import org.springframework.context.annotation.Bean; 18 | import org.springframework.context.annotation.Configuration; 19 | 20 | @Configuration 21 | @EnableConfigurationProperties(AtomsProperties.class) 22 | @ConditionalOnClass(AtomsBean.class) 23 | @ConditionalOnProperty(prefix = "atoms", value = "enabled", matchIfMissing = true) 24 | public class AtomsAutoConfiguration { 25 | 26 | @Autowired 27 | private AtomsProperties atomsProperties; 28 | 29 | @Bean 30 | @ConditionalOnMissingBean(AtomsBean.class) 31 | public CacheChannel initAtoms() { 32 | AtomsBean atomsBean = atomsProperties; 33 | AtomsSpringConfig atomsSpringConfig = new AtomsSpringConfig(); 34 | atomsSpringConfig.setAtomsBean(atomsBean); 35 | return CacheChannel.getInstance(); 36 | } 37 | } -------------------------------------------------------------------------------- /atoms-serializer/atoms-serializer-api/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | 4 | com.lovver.atoms 5 | atoms-serializer 6 | 1.0.18-SNAPSHOT 7 | 8 | atoms-serializer-api 9 | 10 | 11 | 12 | org.apache.maven.plugins 13 | maven-source-plugin 14 | 3.0.1 15 | 16 | true 17 | 18 | 19 | 20 | compile 21 | 22 | jar 23 | 24 | 25 | 26 | 27 | 28 | 29 | maven-compiler-plugin 30 | 31 | 1.7 32 | 1.7 33 | 34 | 35 | 36 | 37 | 38 | 39 | com.lovver.atoms 40 | atoms-common 41 | 1.0.18-SNAPSHOT 42 | 43 | 44 | com.lovver.atoms 45 | atoms-config 46 | 1.0.18-SNAPSHOT 47 | 48 | 49 | -------------------------------------------------------------------------------- /atoms-broadcast/atoms-broadcast-zookeeper/src/test/java/com/sample/ZookeeperTest.java: -------------------------------------------------------------------------------- 1 | package com.sample; 2 | 3 | import org.apache.zookeeper.*; 4 | 5 | import java.util.Arrays; 6 | import java.util.List; 7 | 8 | /** 9 | * Created by Administrator on 2017/3/17. 10 | */ 11 | public class ZookeeperTest { 12 | // 根节点 13 | public static final String ROOT = "/root-ktv"; 14 | 15 | public static void main(String[] args) throws Exception { 16 | // 创建一个与服务器的连接 17 | ZooKeeper zk = new ZooKeeper("192.168.1.87:2181", 30000, new Watcher() { 18 | // 监控所有被触发的事件 19 | public void process(WatchedEvent event) { 20 | System.out.println("状态:" + event.getState()+":"+event.getType()+":"+event.getWrapper()+":"+event.getPath()); 21 | } 22 | }); 23 | // 创建一个总的目录ktv,并不控制权限,这里需要用持久化节点,不然下面的节点创建容易出错 24 | zk.create(ROOT, "root-ktv".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); 25 | 26 | // 然后杭州开一个KTV , PERSISTENT_SEQUENTIAL 类型会自动加上 0000000000 自增的后缀 27 | zk.create(ROOT+"/杭州KTV", "杭州KTV".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL); 28 | 29 | // 也可以在北京开一个, EPHEMERAL session 过期了就会自动删除 30 | zk.create(ROOT+"/北京KTV", "北京KTV".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); 31 | 32 | // 同理,我可以在北京开多个,EPHEMERAL_SEQUENTIAL session 过期自动删除,也会加数字的后缀 33 | zk.create(ROOT+"/北京KTV-分店", "北京KTV-分店".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL); 34 | 35 | // 我们也可以 来看看 一共监视了多少家的ktv 36 | List ktvs = zk.getChildren(ROOT, true); 37 | System.out.println(Arrays.toString(ktvs.toArray())); 38 | for(String node : ktvs){ 39 | // 删除节点 40 | zk.delete(ROOT+"/"+node,-1); 41 | } 42 | // 根目录得最后删除的 43 | zk.delete(ROOT, -1); 44 | zk.close(); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /atoms-context/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | 4 | com.lovver.atoms 5 | atoms-parent 6 | 1.0.18-SNAPSHOT 7 | 8 | atoms-context 9 | 10 | 11 | 12 | org.apache.maven.plugins 13 | maven-source-plugin 14 | 3.0.1 15 | 16 | true 17 | 18 | 19 | 20 | compile 21 | 22 | jar 23 | 24 | 25 | 26 | 27 | 28 | 29 | maven-compiler-plugin 30 | 31 | 1.7 32 | 1.7 33 | 34 | 35 | 36 | 37 | 38 | 39 | com.lovver.atoms 40 | atoms-config 41 | 1.0.18-SNAPSHOT 42 | 43 | 44 | com.lovver.atoms 45 | atoms-cache-api 46 | 1.0.18-SNAPSHOT 47 | 48 | 49 | com.lovver.atoms 50 | atoms-broadcast-api 51 | 1.0.18-SNAPSHOT 52 | 53 | 54 | com.lovver.atoms 55 | atoms-serializer-api 56 | 1.0.18-SNAPSHOT 57 | 58 | 59 | -------------------------------------------------------------------------------- /atoms-common/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.lovver.atoms 6 | atoms-parent 7 | 1.0.18-SNAPSHOT 8 | 9 | atoms-common 10 | 11 | 12 | 13 | org.apache.maven.plugins 14 | maven-source-plugin 15 | 3.0.1 16 | 17 | true 18 | 19 | 20 | 21 | compile 22 | 23 | jar 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | commons-lang 33 | commons-lang 34 | 2.6 35 | 36 | 37 | com.thoughtworks.xstream 38 | xstream 39 | 1.4.9 40 | 41 | 42 | org.slf4j 43 | slf4j-log4j12 44 | 1.6.1 45 | 46 | 47 | com.alibaba 48 | fastjson 49 | 1.2.12 50 | 51 | 52 | -------------------------------------------------------------------------------- /atoms-broadcast/atoms-broadcast-redis/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | 4 | com.lovver.atoms 5 | atoms-broadcast 6 | 1.0.18-SNAPSHOT 7 | 8 | atoms-broadcast-redis 9 | 10 | 11 | 12 | org.apache.maven.plugins 13 | maven-source-plugin 14 | 3.0.1 15 | 16 | true 17 | 18 | 19 | 20 | compile 21 | 22 | jar 23 | 24 | 25 | 26 | 27 | 28 | 29 | maven-compiler-plugin 30 | 31 | 1.7 32 | 1.7 33 | 34 | 35 | 36 | 37 | 38 | 39 | redis.clients 40 | jedis 41 | 2.8.0 42 | 43 | 44 | com.lovver.atoms 45 | atoms-broadcast-api 46 | 1.0.18-SNAPSHOT 47 | 48 | 49 | com.lovver.atoms 50 | atoms-context 51 | 1.0.18-SNAPSHOT 52 | 53 | 54 | -------------------------------------------------------------------------------- /atoms-core/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | 4 | com.lovver.atoms 5 | atoms-parent 6 | 1.0.18-SNAPSHOT 7 | 8 | atoms-core 9 | 10 | 11 | 12 | org.apache.maven.plugins 13 | maven-source-plugin 14 | 3.0.1 15 | 16 | true 17 | 18 | 19 | 20 | compile 21 | 22 | jar 23 | 24 | 25 | 26 | 27 | 28 | 29 | maven-compiler-plugin 30 | 31 | 1.7 32 | 1.7 33 | 34 | 35 | 36 | 37 | 38 | 39 | com.lovver.atoms 40 | atoms-cache-api 41 | 1.0.18-SNAPSHOT 42 | 43 | 44 | com.lovver.atoms 45 | atoms-config 46 | 1.0.18-SNAPSHOT 47 | 48 | 49 | com.lovver.atoms 50 | atoms-serializer-api 51 | 1.0.18-SNAPSHOT 52 | 53 | 54 | com.lovver.atoms 55 | atoms-broadcast-api 56 | 1.0.18-SNAPSHOT 57 | 58 | 59 | com.lovver.atoms 60 | atoms-context 61 | 1.0.18-SNAPSHOT 62 | 63 | 64 | -------------------------------------------------------------------------------- /atoms-config/src/main/java/com/lovver/atoms/config/AtomsCacheBean.java: -------------------------------------------------------------------------------- 1 | package com.lovver.atoms.config; 2 | 3 | import com.thoughtworks.xstream.annotations.XStreamAlias; 4 | import com.thoughtworks.xstream.annotations.XStreamAsAttribute; 5 | 6 | @XStreamAlias("cache") 7 | public class AtomsCacheBean { 8 | @XStreamAsAttribute 9 | private String level; 10 | @XStreamAsAttribute 11 | private String type; 12 | // @XStreamAsAttribute 13 | // private String expiredOperator; 14 | // @XStreamAsAttribute 15 | // private String waitTime; 16 | // @XStreamAsAttribute 17 | // private String delete_atom="true"; 18 | 19 | private AtomsCacheConfigBean cacheConfig; 20 | 21 | private AtomsCacheTTLBean cacheTTL; 22 | 23 | public AtomsCacheTTLBean getCacheTTL() { 24 | return cacheTTL; 25 | } 26 | 27 | public void setCacheTTL(AtomsCacheTTLBean cacheTTL) { 28 | this.cacheTTL = cacheTTL; 29 | } 30 | 31 | public String getLevel() { 32 | return level; 33 | } 34 | 35 | public void setLevel(String level) { 36 | this.level = level; 37 | } 38 | 39 | public String getType() { 40 | return type; 41 | } 42 | 43 | public void setType(String type) { 44 | this.type = type; 45 | } 46 | 47 | public AtomsCacheConfigBean getCacheConfig() { 48 | return cacheConfig; 49 | } 50 | 51 | public void setCacheConfig(AtomsCacheConfigBean cacheConfig) { 52 | this.cacheConfig = cacheConfig; 53 | } 54 | 55 | // public String getExpiredOperator() { 56 | // return expiredOperator; 57 | // } 58 | // 59 | // public void setExpiredOperator(String expiredOperator) { 60 | // this.expiredOperator = expiredOperator; 61 | // } 62 | // 63 | // public String getWaitTime() { 64 | // return waitTime; 65 | // } 66 | // 67 | // public void setWaitTime(String waitTime) { 68 | // this.waitTime = waitTime; 69 | // } 70 | 71 | // public String getDelete_atom() { 72 | // if(StringUtils.isEmpty(delete_atom)){ 73 | // delete_atom="true"; 74 | // } 75 | // return delete_atom; 76 | // } 77 | // 78 | // public void setDelete_atom(String delete_atom) { 79 | // this.delete_atom = delete_atom; 80 | // } 81 | 82 | } 83 | -------------------------------------------------------------------------------- /atoms-cache/atoms-cache-ssdb/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | 4 | com.lovver.atoms 5 | atoms-cache 6 | 1.0.18-SNAPSHOT 7 | 8 | atoms-cache-ssdb 9 | 10 | 11 | 12 | org.apache.maven.plugins 13 | maven-source-plugin 14 | 3.0.1 15 | 16 | true 17 | 18 | 19 | 20 | compile 21 | 22 | jar 23 | 24 | 25 | 26 | 27 | 28 | 29 | maven-compiler-plugin 30 | 31 | 1.7 32 | 1.7 33 | 34 | 35 | 36 | 37 | 38 | 39 | com.lovver.atoms 40 | atoms-cache-api 41 | 1.0.18-SNAPSHOT 42 | 43 | 44 | com.lovver.ssdbj 45 | ssdbj-core 46 | 0.0.2 47 | 48 | 49 | com.lovver.atoms 50 | atoms-context 51 | 1.0.18-SNAPSHOT 52 | 53 | 54 | -------------------------------------------------------------------------------- /atoms-broadcast/atoms-broadcast-jgroups/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.lovver.atoms 6 | atoms-broadcast 7 | 1.0.18-SNAPSHOT 8 | 9 | atoms-broadcast-jgroups 10 | 11 | 12 | 13 | org.apache.maven.plugins 14 | maven-source-plugin 15 | 3.0.1 16 | 17 | true 18 | 19 | 20 | 21 | compile 22 | 23 | jar 24 | 25 | 26 | 27 | 28 | 29 | 30 | maven-compiler-plugin 31 | 32 | 1.7 33 | 1.7 34 | 35 | 36 | 37 | 38 | 39 | 40 | org.jgroups 41 | jgroups 42 | 3.6.7.Final 43 | 44 | 45 | com.lovver.atoms 46 | atoms-broadcast-api 47 | 1.0.18-SNAPSHOT 48 | 49 | 50 | com.lovver.atoms 51 | atoms-context 52 | 1.0.18-SNAPSHOT 53 | 54 | 55 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | com.lovver.atoms 5 | atoms-parent 6 | 1.0.18-SNAPSHOT 7 | pom 8 | 9 | 10 | 11 | org.apache.maven.plugins 12 | maven-compiler-plugin 13 | 3.1 14 | 15 | 1.7 16 | 1.7 17 | UTF8 18 | 19 | 20 | 21 | 22 | 23 | 1.4.7.RELEASE 24 | 4.3.9.RELEASE 25 | 26 | 27 | atoms-common 28 | atoms-broadcast 29 | atoms-config 30 | atoms-serializer 31 | atoms 32 | atoms-cache 33 | atoms-core 34 | atoms-context 35 | atoms-spring 36 | atoms-springboot 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | Releases 51 | Local Nexus Repository 52 | http://219.232.239.60:8081/nexus/content/repositories/releases 53 | 54 | 55 | Snapshots 56 | Local Nexus Repository 57 | http://219.232.239.60:8081/nexus/content/repositories/snapshots 58 | 59 | 60 | -------------------------------------------------------------------------------- /atoms-cache/atoms-cache-api/src/main/java/com/lovver/atoms/cache/Cache.java: -------------------------------------------------------------------------------- 1 | package com.lovver.atoms.cache; 2 | 3 | import java.util.List; 4 | 5 | import com.lovver.atoms.common.exception.CacheException; 6 | 7 | /** 8 | * Implementors define a caching algorithm. All implementors 9 | * must be threadsafe. 10 | * @author liudong 11 | */ 12 | public interface Cache { 13 | 14 | /** 15 | * Get an item from the cache, nontransactionally 16 | * @param key cache key 17 | * @return the cached object or null 18 | */ 19 | public Object get(Object key) throws CacheException; 20 | 21 | /** 22 | * Add an item to the cache, nontransactionally, with 23 | * failfast semantics 24 | * @param key cache key 25 | * @param value cache value 26 | */ 27 | public void put(Object key, Object value) throws CacheException; 28 | public void put(Object key, Object value,boolean broadFlg) throws CacheException; 29 | 30 | public void put(Object key, Object value,Integer expiretime) throws CacheException; 31 | public void put(Object key, Object value,Integer expiretime,boolean broadFlg) throws CacheException; 32 | 33 | /** 34 | * Add an item to the cache 35 | * @param key cache key 36 | * @param value cache value 37 | */ 38 | public void update(Object key, Object value) throws CacheException; 39 | 40 | // /** 41 | // * 上一级失效更新,不做通知处理 42 | // * 43 | // * @param key 44 | // * @param value 45 | // * @throws CacheException 46 | // */ 47 | // public void expireUpdate(Object key, Object value) throws CacheException; 48 | 49 | 50 | @SuppressWarnings("rawtypes") 51 | public List keys() throws CacheException ; 52 | 53 | /** 54 | * @param key Cache key 55 | * Remove an item from the cache 56 | */ 57 | public void evict(Object key) throws CacheException; 58 | public void evict(Object key,boolean broadFlg) throws CacheException; 59 | /** 60 | * Batch remove cache objects 61 | * @param keys the cache keys to be evicted 62 | */ 63 | @SuppressWarnings("rawtypes") 64 | public void evict(List keys) throws CacheException; 65 | 66 | public void evict(List keys,boolean broadFlg) throws CacheException; 67 | 68 | /** 69 | * Clear the cache 70 | */ 71 | public void clear() throws CacheException; 72 | 73 | public void clear(boolean broadFlg) throws CacheException; 74 | 75 | /** 76 | * Clean up 77 | */ 78 | public void destroy() throws CacheException; 79 | 80 | } 81 | -------------------------------------------------------------------------------- /atoms/src/test/resources/network.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 51 | 52 | 55 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | -------------------------------------------------------------------------------- /atoms-config/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | 4 | com.lovver.atoms 5 | atoms-parent 6 | 1.0.18-SNAPSHOT 7 | 8 | atoms-config 9 | 10 | 11 | 12 | org.apache.maven.plugins 13 | maven-source-plugin 14 | 3.0.1 15 | 16 | true 17 | 18 | 19 | 20 | compile 21 | 22 | jar 23 | 24 | 25 | 26 | 27 | 28 | 29 | maven-compiler-plugin 30 | 31 | 1.7 32 | 1.7 33 | 34 | 35 | 36 | 37 | 38 | 39 | com.lovver.atoms 40 | atoms-common 41 | 1.0.18-SNAPSHOT 42 | 43 | 44 | org.springframework 45 | spring-core 46 | ${spring.version} 47 | test 48 | 49 | 50 | org.springframework 51 | spring-beans 52 | ${spring.version} 53 | test 54 | 55 | 56 | org.springframework 57 | spring-context 58 | ${spring.version} 59 | test 60 | 61 | 62 | org.springframework 63 | spring-webmvc 64 | ${spring.version} 65 | test 66 | 67 | 68 | -------------------------------------------------------------------------------- /atoms-spring/src/main/java/com/lovver/atoms/spring/cache/SpringAtomsCache.java: -------------------------------------------------------------------------------- 1 | package com.lovver.atoms.spring.cache; 2 | 3 | 4 | import com.lovver.atoms.config.AtomsSpringConfig; 5 | import com.lovver.atoms.core.CacheChannel; 6 | import org.springframework.cache.Cache; 7 | import org.springframework.cache.support.SimpleValueWrapper; 8 | 9 | import java.util.concurrent.Callable; 10 | 11 | public class SpringAtomsCache implements Cache{ 12 | 13 | private String name; 14 | 15 | public SpringAtomsCache(){} 16 | 17 | public SpringAtomsCache(String name){ 18 | this.name=name; 19 | } 20 | 21 | public void setName(String name) { 22 | this.name = name; 23 | } 24 | 25 | @Override 26 | public String getName() { 27 | return this.name; 28 | } 29 | 30 | @Override 31 | public Object getNativeCache() { 32 | return channel; 33 | } 34 | 35 | 36 | @Override 37 | public void put(Object key, Object value) { 38 | channel.set(this.name,key,value); 39 | } 40 | 41 | @Override 42 | public ValueWrapper putIfAbsent(Object key, Object value) { 43 | Object val=channel.get(this.name,key); 44 | if(val!=null){ 45 | return new SimpleValueWrapper(val); 46 | } 47 | return new SimpleValueWrapper(value); 48 | } 49 | 50 | @Override 51 | public void evict(Object key) { 52 | channel.evict(this.name,key); 53 | } 54 | 55 | @Override 56 | public void clear() { 57 | channel.clear(this.name); 58 | } 59 | 60 | private AtomsSpringConfig atomsSpringConfig; 61 | private static CacheChannel channel=null; 62 | 63 | public void setAtomsSpringConfig(AtomsSpringConfig atomsSpringConfig) { 64 | this.atomsSpringConfig = atomsSpringConfig; 65 | channel=CacheChannel.getInstance(); 66 | } 67 | 68 | public AtomsSpringConfig getAtomsSpringConfig() { 69 | return atomsSpringConfig; 70 | } 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | @Override 79 | public ValueWrapper get(Object key) { 80 | Object val=channel.get(this.name,key); 81 | if(val==null){ 82 | return null; 83 | } 84 | return new SimpleValueWrapper(val); 85 | } 86 | 87 | @Override 88 | public T get(Object key, Class aClass) { 89 | Object val=channel.get(this.name,key); 90 | if(val==null){ 91 | return null; 92 | } 93 | return (T) val; 94 | } 95 | 96 | @Override 97 | public T get(Object key, Callable callable) { 98 | 99 | Object val=channel.get(this.name,key); 100 | if(val!=null){ 101 | return (T)val; 102 | } 103 | try { 104 | return callable.call(); 105 | } catch (Exception e) { 106 | e.printStackTrace(); 107 | } 108 | return null; 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /atoms/src/test/resources/ehcache.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 23 | 29 | 30 | 31 | 32 | 50 | 51 | 58 | -------------------------------------------------------------------------------- /atoms-config/src/test/resources/ehcache.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 23 | 29 | 30 | 31 | 32 | 50 | 51 | 58 | 59 | -------------------------------------------------------------------------------- /atoms-springboot/atoms-spring-boot-test/src/main/resources/ehcache.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 23 | 29 | 30 | 31 | 32 | 50 | 51 | 58 | -------------------------------------------------------------------------------- /atoms-broadcast/atoms-broadcast-zookeeper/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.lovver.atoms 6 | atoms-broadcast 7 | 1.0.18-SNAPSHOT 8 | 9 | atoms-broadcast-zookeeper 10 | 11 | 12 | 13 | maven-compiler-plugin 14 | 15 | 1.7 16 | 1.7 17 | 18 | 19 | 20 | org.apache.maven.plugins 21 | maven-source-plugin 22 | 3.0.1 23 | 24 | true 25 | 26 | 27 | 28 | compile 29 | 30 | jar 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | org.apache.zookeeper 41 | zookeeper 42 | 3.3.3 43 | 44 | 45 | netty 46 | io.netty 47 | 48 | 49 | log4j 50 | log4j 51 | 52 | 53 | 54 | 55 | com.lovver.zkclient 56 | zkclient 57 | 1.0.0 58 | 59 | 60 | slf4j-api 61 | org.slf4j 62 | 63 | 64 | log4j-over-slf4j 65 | org.slf4j 66 | 67 | 68 | 69 | 70 | com.lovver.atoms 71 | atoms-broadcast-api 72 | 1.0.18-SNAPSHOT 73 | 74 | 75 | com.lovver.atoms 76 | atoms-context 77 | 1.0.18-SNAPSHOT 78 | 79 | 80 | -------------------------------------------------------------------------------- /atoms-config/src/main/java/com/lovver/atoms/config/AtomsConfig.java: -------------------------------------------------------------------------------- 1 | package com.lovver.atoms.config; 2 | 3 | import java.io.IOException; 4 | import java.io.InputStream; 5 | import java.util.Enumeration; 6 | import java.util.List; 7 | import java.util.Properties; 8 | 9 | import org.apache.commons.lang.StringUtils; 10 | 11 | import com.thoughtworks.xstream.XStream; 12 | import org.slf4j.Logger; 13 | import org.slf4j.LoggerFactory; 14 | 15 | public class AtomsConfig { 16 | private final static Logger log = LoggerFactory.getLogger(AtomsConfig.class); 17 | 18 | protected static AtomsBean atomsBean; 19 | 20 | public static AtomsBean getAtomsConfig(){ 21 | return atomsBean; 22 | } 23 | 24 | static { 25 | InputStream is = AtomsConfig.class.getClassLoader() 26 | .getResourceAsStream("atoms.xml"); 27 | if(is!=null){ 28 | xmlToJavaBean(is); 29 | mergeAtomsProperty(atomsBean); 30 | try { 31 | is.close(); 32 | } catch (IOException e) { 33 | e.printStackTrace(); 34 | } 35 | } 36 | } 37 | 38 | public static void mergeAtomsProperty(AtomsBean atomsBean){ 39 | InputStream is = AtomsConfig.class.getClassLoader().getResourceAsStream("atoms.properties"); 40 | if(is!=null){ 41 | try { 42 | List lstCache= atomsBean.getCache(); 43 | Properties prop=new Properties(); 44 | prop.load(is); 45 | for(AtomsCacheBean cacheBean:lstCache){ 46 | mergeCacheConfig(prop,cacheBean); 47 | } 48 | } catch (IOException e) { 49 | e.printStackTrace(); 50 | }finally { 51 | try { 52 | is.close(); 53 | } catch (IOException e) { 54 | e.printStackTrace(); 55 | } 56 | } 57 | } 58 | } 59 | 60 | private static void mergeCacheConfig(Properties prop,AtomsCacheBean cacheBean){ 61 | String level=cacheBean.getLevel(); 62 | String host=prop.getProperty("cache."+level+".host"); 63 | if(StringUtils.isNotEmpty(host)){ 64 | cacheBean.getCacheConfig().setHost(host); 65 | } 66 | 67 | String port=prop.getProperty("cache."+level+".port"); 68 | if(StringUtils.isNotEmpty(port)){ 69 | cacheBean.getCacheConfig().setPort(port); 70 | } 71 | 72 | String timeout=prop.getProperty("cache."+level+".timeout"); 73 | if(StringUtils.isNotEmpty(timeout)){ 74 | cacheBean.getCacheConfig().setTimeout(timeout); 75 | } 76 | } 77 | /** 78 | * 把xml转化为java对象 79 | */ 80 | public static void xmlToJavaBean(InputStream is) { 81 | XStream stream = new XStream(); 82 | stream.autodetectAnnotations(true); 83 | try { 84 | stream.alias("atoms", AtomsBean.class); 85 | atomsBean = (AtomsBean) stream.fromXML(is); 86 | List lstAtomsCacheBean=atomsBean.getCache(); 87 | for(AtomsCacheBean atomsCacheBean: lstAtomsCacheBean){ 88 | AtomsCacheTTLBean atomsCacheTTLBean=atomsCacheBean.getCacheTTL(); 89 | if(!atomsCacheBean.getLevel().equals("1")&&atomsCacheTTLBean!=null&&(atomsCacheTTLBean.getLstTTL()!=null||atomsCacheTTLBean.getLstTTL().size()>0)){ 90 | log.error("do not config ttl in level > 1"); 91 | throw new RuntimeException("ttl config only in level 1 cache,there is a ttl config in other level cache"); 92 | } 93 | } 94 | } catch (Exception e) { 95 | e.printStackTrace(); 96 | } 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /atoms-broadcast/atoms-broadcast-redis/src/test/java/com/sample/redis/RedisClient.java: -------------------------------------------------------------------------------- 1 | package com.sample.redis; 2 | 3 | import java.util.Date; 4 | import java.util.List; 5 | 6 | import org.apache.commons.lang.RandomStringUtils; 7 | import org.apache.commons.lang.time.DateFormatUtils; 8 | 9 | import redis.clients.jedis.Jedis; 10 | import redis.clients.jedis.JedisPubSub; 11 | import redis.clients.jedis.Pipeline; 12 | import redis.clients.jedis.Response; 13 | import redis.clients.jedis.Transaction; 14 | 15 | public class RedisClient { 16 | private Jedis jedis;// 17 | RedisClient(String host,int port){ 18 | jedis = new Jedis(host,port); 19 | } 20 | 21 | public void pipeline(){ 22 | String key = "pipeline-test"; 23 | String old = jedis.get(key); 24 | if(old != null){ 25 | System.out.println("Key:" + key + ",old value:" + old); 26 | } 27 | //代码模式1,这种模式是最常见的方式 28 | Pipeline p1 = jedis.pipelined(); 29 | p1.incr(key); 30 | System.out.println("Request incr"); 31 | p1.incr(key); 32 | System.out.println("Request incr"); 33 | //结束pipeline,并开始从相应中获得数据 34 | List responses = p1.syncAndReturnAll(); 35 | if(responses == null || responses.isEmpty()){ 36 | throw new RuntimeException("Pipeline error: no response..."); 37 | } 38 | for(Object resp : responses){ 39 | System.out.println("Response:" + resp.toString());//注意,此处resp的类型为Long 40 | } 41 | //代码模式2 42 | Pipeline p2 = jedis.pipelined(); 43 | Response r1 = p2.incr(key); 44 | try{ 45 | r1.get(); 46 | }catch(Exception e){ 47 | System.out.println("Error,you cant get() before sync,because IO of response hasn't begin.."); 48 | } 49 | Response r2 = p2.incr(key); 50 | p2.sync(); 51 | System.out.println("Pipeline,mode 2,--->" + r1.get()); 52 | System.out.println("Pipeline,mode 2,--->" + r2.get()); 53 | 54 | } 55 | 56 | public void txPipeline(){ 57 | String key = "pipeline-test"; 58 | String old = jedis.get(key); 59 | if(old != null){ 60 | System.out.println("Key:" + key + ",old value:" + old); 61 | } 62 | //代码模式1,这种模式是最常见的方式 63 | Pipeline p1 = jedis.pipelined(); 64 | //p1.multi(); 65 | p1.incr(key); 66 | System.out.println("Request incr"); 67 | p1.incr(key); 68 | //System.out.println(jedis.get(key)); 69 | jedis.set("txptest", "1"); 70 | System.out.println("Request incr"); 71 | //Response> txresult= p1.exec(); 72 | p1.sync(); 73 | //结束pipeline,并开始从相应中获得数据 74 | // List responses = txresult.get(); 75 | // if(responses == null || responses.isEmpty()){ 76 | // throw new RuntimeException("Pipeline error: no response..."); 77 | // } 78 | // for(Object resp : responses){ 79 | // System.out.println("Response:" + resp.toString());//注意,此处resp的类型为Long 80 | // } 81 | } 82 | 83 | public void transaction(){ 84 | String key = "transaction-key"; 85 | jedis.set(key, "20"); 86 | jedis.watch(key); 87 | Transaction tx = jedis.multi(); 88 | tx.incr(key); 89 | tx.incr(key); 90 | tx.incr(key); 91 | List result = tx.exec(); 92 | if(result == null || result.isEmpty()){ 93 | System.out.println("Transaction error...");//可能是watch-key被外部修改,或者是数据操作被驳回 94 | return; 95 | } 96 | for(Object rt : result){ 97 | System.out.println(rt.toString()); 98 | } 99 | } 100 | 101 | /** 102 | * @param args 103 | */ 104 | public static void main(String[] args) throws Exception { 105 | RedisClient client = new RedisClient(Constants.host, Constants.port); 106 | // client.pipeline(); 107 | // client.pipeline(); 108 | // client.txPipeline(); 109 | // client.transaction(); 110 | } 111 | 112 | } 113 | -------------------------------------------------------------------------------- /atoms-cache/atoms-cache-redis/src/main/java/com/lovver/atoms/cache/redis/RedisCacheEventListener.java: -------------------------------------------------------------------------------- 1 | package com.lovver.atoms.cache.redis; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.lovver.atoms.broadcast.BroadCast; 5 | import com.lovver.atoms.broadcast.Command; 6 | import com.lovver.atoms.cache.Cache; 7 | import com.lovver.atoms.cache.CacheEventListener; 8 | import com.lovver.atoms.cache.CacheProvider; 9 | import com.lovver.atoms.common.annotation.SPI; 10 | import com.lovver.atoms.common.exception.CacheException; 11 | import com.lovver.atoms.context.AtomsContext; 12 | import com.lovver.atoms.serializer.Serializer; 13 | import org.slf4j.Logger; 14 | import org.slf4j.LoggerFactory; 15 | 16 | import java.util.Map; 17 | 18 | @SPI("redis") 19 | public class RedisCacheEventListener implements CacheEventListener { 20 | private final static Logger log = LoggerFactory.getLogger(RedisCacheEventListener.class); 21 | private Map mCacheProvider=AtomsContext.getCacheProvider(); 22 | 23 | public void notifyElementExpired(String region, Object key) { 24 | log.debug("notifyElementExpired["+region+"]["+key+"]"); 25 | for(int i=2;i<=mCacheProvider.size();i++) { 26 | Cache cache = AtomsContext.getCache(region, i); 27 | cache.evict(key); 28 | } 29 | if(null!=broadCast){ 30 | Command cmd=new Command(Command.OPT_DELETE_KEY,region,key); 31 | broadCast.broadcast(cmd.toBuffers()); 32 | } 33 | } 34 | 35 | @Override 36 | public void notifyElementRemoved(String region, Object key) 37 | throws CacheException { 38 | log.debug("notifyElementRemoved["+region+"]["+key+"]"); 39 | if(null!=broadCast){ 40 | Command cmd=new Command(Command.OPT_DELETE_KEY,region,key); 41 | broadCast.broadcast(cmd.toBuffers()); 42 | } 43 | } 44 | 45 | @Override 46 | public void notifyElementEvicted(String region, Object key) { 47 | log.debug("notifyElementEvicted["+region+"]["+key+"]"); 48 | if(null!=broadCast){ 49 | Command cmd=new Command(Command.OPT_DELETE_KEY,region,key); 50 | broadCast.broadcast(cmd.toBuffers()); 51 | } 52 | } 53 | 54 | @Override 55 | public void notifyRemoveAll(String region) { 56 | log.debug("notifyRemoveAll["+region+"]"); 57 | if(null!=broadCast){ 58 | Command cmd=new Command(Command.OPT_CLEAR_KEY,region,""); 59 | broadCast.broadcast(cmd.toBuffers()); 60 | } 61 | } 62 | 63 | @Override 64 | public void notifyElementPut(String region, Object key, Object value) 65 | throws CacheException { 66 | log.debug("notifyElementPut["+region+"]["+key+"]"); 67 | if (null != broadCast) { 68 | Command cmd = new Command(Command.OPT_PUT_KEY, region, key,value); 69 | broadCast.broadcast(cmd.toBuffers()); 70 | } 71 | } 72 | 73 | @Override 74 | public void notifyElementPut(String region, Object key, Object value, int expiretime) throws CacheException { 75 | log.debug("notifyElementPut["+region+"]["+key+"]"); 76 | if (null != broadCast) { 77 | Command cmd = new Command(Command.OPT_PUT_KEY, region, key,value,expiretime); 78 | broadCast.broadcast(cmd.toBuffers()); 79 | } 80 | } 81 | 82 | @Override 83 | public void notifyElementUpdated(String region, Object key, Object value) 84 | throws CacheException { 85 | log.debug("notifyElementUpdated["+region+"]"); 86 | if (null != broadCast) { 87 | Command cmd = new Command(Command.OPT_PUT_KEY, region, key,value); 88 | broadCast.broadcast(cmd.toBuffers()); 89 | } 90 | } 91 | 92 | 93 | 94 | 95 | 96 | private int level; 97 | @Override 98 | public void init(int level) { 99 | this.level=level; 100 | broadCast=AtomsContext.getBroadCast(); 101 | } 102 | private static BroadCast broadCast; 103 | } 104 | -------------------------------------------------------------------------------- /atoms-cache/atoms-cache-ssdb/src/main/java/com/lovver/atoms/cache/ssdb/SsdbCacheEventListener.java: -------------------------------------------------------------------------------- 1 | package com.lovver.atoms.cache.ssdb; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.lovver.atoms.broadcast.BroadCast; 5 | import com.lovver.atoms.broadcast.Command; 6 | import com.lovver.atoms.cache.Cache; 7 | import com.lovver.atoms.cache.CacheEventListener; 8 | import com.lovver.atoms.cache.CacheProvider; 9 | import com.lovver.atoms.common.annotation.SPI; 10 | import com.lovver.atoms.common.exception.CacheException; 11 | import com.lovver.atoms.context.AtomsContext; 12 | import com.lovver.atoms.serializer.Serializer; 13 | import org.slf4j.Logger; 14 | import org.slf4j.LoggerFactory; 15 | 16 | import java.util.Map; 17 | 18 | @SPI("ssdb") 19 | public class SsdbCacheEventListener implements CacheEventListener { 20 | 21 | private final static Logger log = LoggerFactory.getLogger(SsdbCacheEventListener.class); 22 | private Map mCacheProvider=AtomsContext.getCacheProvider(); 23 | 24 | public void notifyElementExpired(String region, Object key) { 25 | log.debug("notifyElementExpired["+region+"]["+key+"]"); 26 | for(int i=2;i<=mCacheProvider.size();i++) { 27 | Cache cache = AtomsContext.getCache(region, i); 28 | cache.evict(key); 29 | } 30 | if(null!=broadCast){ 31 | Command cmd=new Command(Command.OPT_DELETE_KEY,region,key); 32 | broadCast.broadcast(cmd.toBuffers()); 33 | } 34 | } 35 | 36 | @Override 37 | public void notifyElementRemoved(String region, Object key) 38 | throws CacheException { 39 | log.debug("notifyElementRemoved["+region+"]["+key+"]"); 40 | if(null!=broadCast){ 41 | Command cmd=new Command(Command.OPT_DELETE_KEY,region,key); 42 | broadCast.broadcast(cmd.toBuffers()); 43 | } 44 | } 45 | 46 | @Override 47 | public void notifyElementEvicted(String region, Object key) { 48 | log.debug("notifyElementEvicted["+region+"]["+key+"]"); 49 | if(null!=broadCast){ 50 | Command cmd=new Command(Command.OPT_DELETE_KEY,region,key); 51 | broadCast.broadcast(cmd.toBuffers()); 52 | } 53 | } 54 | 55 | @Override 56 | public void notifyRemoveAll(String region) { 57 | log.debug("notifyRemoveAll["+region+"]"); 58 | if(null!=broadCast){ 59 | Command cmd=new Command(Command.OPT_CLEAR_KEY,region,""); 60 | broadCast.broadcast(cmd.toBuffers()); 61 | } 62 | } 63 | 64 | @Override 65 | public void notifyElementPut(String region, Object key, Object value) 66 | throws CacheException { 67 | log.debug("notifyElementPut["+region+"]["+key+"]"); 68 | if (null != broadCast) { 69 | Command cmd = new Command(Command.OPT_PUT_KEY, region, key,value); 70 | broadCast.broadcast(cmd.toBuffers()); 71 | } 72 | } 73 | 74 | @Override 75 | public void notifyElementPut(String region, Object key, Object value, int expiretime) throws CacheException { 76 | log.debug("notifyElementPut["+region+"]["+key+"]"); 77 | if (null != broadCast) { 78 | Command cmd = new Command(Command.OPT_PUT_KEY, region, key,value,expiretime); 79 | broadCast.broadcast(cmd.toBuffers()); 80 | } 81 | } 82 | 83 | @Override 84 | public void notifyElementUpdated(String region, Object key, Object value) 85 | throws CacheException { 86 | log.debug("notifyElementUpdated["+region+"]"); 87 | if (null != broadCast) { 88 | Command cmd = new Command(Command.OPT_PUT_KEY, region, key,value); 89 | broadCast.broadcast(cmd.toBuffers()); 90 | } 91 | } 92 | 93 | 94 | 95 | 96 | 97 | private int level; 98 | @Override 99 | public void init(int level) { 100 | this.level=level; 101 | broadCast=AtomsContext.getBroadCast(); 102 | } 103 | private static BroadCast broadCast; 104 | } 105 | -------------------------------------------------------------------------------- /atoms-broadcast/atoms-broadcast-redis/src/test/java/com/sample/redis/pubsub/persistent/PPrintListener.java: -------------------------------------------------------------------------------- 1 | package com.sample.redis.pubsub.persistent; 2 | 3 | import java.util.Date; 4 | 5 | import org.apache.commons.lang.time.DateFormatUtils; 6 | 7 | import com.sample.redis.Constants; 8 | 9 | import redis.clients.jedis.Jedis; 10 | import redis.clients.jedis.JedisPubSub; 11 | 12 | public class PPrintListener extends JedisPubSub{ 13 | 14 | private String clientId; 15 | private PSubHandler handler; 16 | 17 | public PPrintListener(String clientId,Jedis jedis){ 18 | this.clientId = clientId; 19 | handler = new PSubHandler(jedis); 20 | } 21 | 22 | @Override 23 | public void onMessage(String channel, String message) { 24 | //此处我们可以取消订阅 25 | if(message.equalsIgnoreCase("quit")){ 26 | this.unsubscribe(channel); 27 | } 28 | handler.handle(channel, message); 29 | } 30 | 31 | private void message(String channel,String message){ 32 | String time = DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"); 33 | System.out.println("message receive:" + message + ",channel:" + channel + "..." + time); 34 | } 35 | 36 | @Override 37 | public void onPMessage(String pattern, String channel, String message) { 38 | System.out.println("message receive:" + message + ",pattern channel:" + channel); 39 | 40 | } 41 | 42 | @Override 43 | public void onSubscribe(String channel, int subscribedChannels) { 44 | handler.subscribe(channel); 45 | System.out.println("subscribe:" + channel + ";total channels : " + subscribedChannels); 46 | 47 | } 48 | 49 | @Override 50 | public void onUnsubscribe(String channel, int subscribedChannels) { 51 | handler.unsubscribe(channel); 52 | System.out.println("unsubscribe:" + channel + ";total channels : " + subscribedChannels); 53 | 54 | } 55 | 56 | @Override 57 | public void onPUnsubscribe(String pattern, int subscribedChannels) { 58 | System.out.println("unsubscribe pattern:" + pattern + ";total channels : " + subscribedChannels); 59 | 60 | } 61 | 62 | @Override 63 | public void onPSubscribe(String pattern, int subscribedChannels) { 64 | System.out.println("subscribe pattern:" + pattern + ";total channels : " + subscribedChannels); 65 | } 66 | 67 | @Override 68 | public void unsubscribe(String... channels) { 69 | super.unsubscribe(channels); 70 | for(String channel : channels){ 71 | handler.unsubscribe(channel); 72 | } 73 | } 74 | 75 | class PSubHandler { 76 | 77 | private Jedis jedis; 78 | PSubHandler(Jedis jedis){ 79 | this.jedis = jedis; 80 | } 81 | public void handle(String channel,String message){ 82 | int index = message.indexOf("/"); 83 | if(index < 0){ 84 | return; 85 | } 86 | Long txid = Long.valueOf(message.substring(0,index)); 87 | String key = clientId + "/" + channel; 88 | while(true){ 89 | String lm = jedis.lindex(key, 0);//获取第一个消息 90 | if(lm == null){ 91 | break; 92 | } 93 | int li = lm.indexOf("/"); 94 | //如果消息不合法,删除并处理 95 | if(li < 0){ 96 | String result = jedis.lpop(key);//删除当前message 97 | //为空 98 | if(result == null){ 99 | break; 100 | } 101 | message(channel, lm); 102 | continue; 103 | } 104 | Long lxid = Long.valueOf(lm.substring(0,li));//获取消息的txid 105 | //直接消费txid之前的残留消息 106 | if(txid >= lxid){ 107 | jedis.lpop(key);//删除当前message 108 | message(channel, lm); 109 | continue; 110 | }else{ 111 | break; 112 | } 113 | } 114 | } 115 | 116 | public void subscribe(String channel){ 117 | String key = clientId + "/" + channel; 118 | boolean exist = jedis.sismember(Constants.SUBSCRIBE_CENTER,key); 119 | if(!exist){ 120 | jedis.sadd(Constants.SUBSCRIBE_CENTER, key); 121 | } 122 | } 123 | 124 | public void unsubscribe(String channel){ 125 | String key = clientId + "/" + channel; 126 | jedis.srem(Constants.SUBSCRIBE_CENTER, key);//从“活跃订阅者”集合中删除 127 | jedis.del(key);//删除“订阅者消息队列” 128 | } 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /atoms-cache/atoms-cache-ehcache/src/main/java/com/lovver/atoms/cache/ehcache/EhCacheEventListener.java: -------------------------------------------------------------------------------- 1 | package com.lovver.atoms.cache.ehcache; 2 | 3 | 4 | import com.alibaba.fastjson.JSON; 5 | import com.lovver.atoms.broadcast.BroadCast; 6 | import com.lovver.atoms.broadcast.Command; 7 | import com.lovver.atoms.cache.Cache; 8 | import com.lovver.atoms.cache.CacheEventListener; 9 | import com.lovver.atoms.cache.CacheProvider; 10 | import com.lovver.atoms.common.annotation.SPI; 11 | import com.lovver.atoms.common.exception.CacheException; 12 | import com.lovver.atoms.context.AtomsContext; 13 | import com.lovver.atoms.serializer.Serializer; 14 | import org.slf4j.Logger; 15 | import org.slf4j.LoggerFactory; 16 | 17 | import java.util.List; 18 | import java.util.Map; 19 | 20 | 21 | @SPI("ehcache") 22 | public class EhCacheEventListener implements CacheEventListener { 23 | private final static Logger log = LoggerFactory.getLogger(EhCacheEventListener.class); 24 | private Map mCacheProvider=AtomsContext.getCacheProvider(); 25 | 26 | @Override 27 | public void notifyElementExpired(String region, Object key) { 28 | log.debug("notifyElementExpired["+region+"]["+key+"]"); 29 | for(int i=2;i<=mCacheProvider.size();i++) { 30 | Cache cache = AtomsContext.getCache(region, i); 31 | cache.evict(key); 32 | } 33 | if (null != broadCast) { 34 | Command cmd = new Command(Command.OPT_DELETE_KEY, region, key); 35 | broadCast.broadcast(cmd.toBuffers()); 36 | } 37 | } 38 | 39 | @Override 40 | public void notifyElementRemoved(String region, Object key) 41 | throws CacheException { 42 | log.debug("notifyElementRemoved["+region+"]["+key+"]"); 43 | if (null != broadCast) { 44 | Command cmd = new Command(Command.OPT_DELETE_KEY, region, key); 45 | broadCast.broadcast(cmd.toBuffers()); 46 | } 47 | } 48 | 49 | @Override 50 | public void notifyElementEvicted(String region, Object key) { 51 | log.debug("notifyElementEvicted["+region+"]["+key+"]"); 52 | if (null != broadCast) { 53 | Command cmd = new Command(Command.OPT_DELETE_KEY, region, key); 54 | broadCast.broadcast(cmd.toBuffers()); 55 | } 56 | } 57 | 58 | @Override 59 | public void notifyRemoveAll(String region) { 60 | log.debug("notifyRemoveAll["+region+"]"); 61 | if (null != broadCast) { 62 | Command cmd = new Command(Command.OPT_DELETE_KEY, region, ""); 63 | broadCast.broadcast(cmd.toBuffers()); 64 | } 65 | } 66 | 67 | @Override 68 | public void notifyElementPut(String region, Object key, Object value) 69 | throws CacheException { 70 | log.debug("notifyElementPut["+region+"]["+key+"]"); 71 | if (null != broadCast) { 72 | Command cmd = new Command(Command.OPT_PUT_KEY, region, key,value); 73 | broadCast.broadcast(cmd.toBuffers()); 74 | } 75 | } 76 | 77 | @Override 78 | public void notifyElementPut(String region, Object key, Object value, int expiretime) throws CacheException { 79 | log.debug("notifyElementPut["+region+"]["+key+"]"); 80 | if (null != broadCast) { 81 | Command cmd = new Command(Command.OPT_PUT_KEY, region, key,value,expiretime); 82 | broadCast.broadcast(cmd.toBuffers()); 83 | } 84 | } 85 | 86 | @Override 87 | public void notifyElementUpdated(String region, Object key, Object value) 88 | throws CacheException { 89 | log.debug("notifyElementUpdated["+region+"]"); 90 | if (null != broadCast) { 91 | Command cmd = new Command(Command.OPT_PUT_KEY, region, key,value); 92 | broadCast.broadcast(cmd.toBuffers()); 93 | } 94 | } 95 | 96 | private int level; 97 | 98 | @Override 99 | public void init(int level) { 100 | this.level = level; 101 | broadCast = AtomsContext.getBroadCast(); 102 | } 103 | 104 | private static BroadCast broadCast; 105 | 106 | } 107 | -------------------------------------------------------------------------------- /atoms-broadcast/atoms-broadcast-jgroups/src/main/java/com/lovver/atoms/broadcast/jgroups/JGroupsPubSub.java: -------------------------------------------------------------------------------- 1 | package com.lovver.atoms.broadcast.jgroups; 2 | 3 | import java.io.InputStream; 4 | import java.net.URL; 5 | import java.util.List; 6 | import java.util.concurrent.CopyOnWriteArraySet; 7 | 8 | import com.lovver.atoms.config.AtomsConfig; 9 | import org.jgroups.*; 10 | 11 | import com.alibaba.fastjson.JSON; 12 | import com.lovver.atoms.broadcast.Command; 13 | import com.lovver.atoms.cache.Cache; 14 | import com.lovver.atoms.config.AtomsBroadCastBean; 15 | import com.lovver.atoms.config.AtomsBroadCastConfigBean; 16 | import com.lovver.atoms.context.AtomsContext; 17 | import com.lovver.atoms.serializer.Serializer; 18 | import org.slf4j.Logger; 19 | import org.slf4j.LoggerFactory; 20 | 21 | public class JGroupsPubSub extends ReceiverAdapter { 22 | private final static Logger log = LoggerFactory.getLogger(JGroupsPubSub.class); 23 | private static Serializer serializer = AtomsContext.getSerializer(); 24 | private CopyOnWriteArraySet broadsetConfig=AtomsContext.getBroadsetConfig(); 25 | protected JChannel channel; 26 | 27 | private AtomsBroadCastConfigBean broadcastConfig; 28 | private AtomsBroadCastBean broadcastBean; 29 | private String xmlFile; 30 | 31 | public JGroupsPubSub(final AtomsBroadCastBean broadcastBean) { 32 | 33 | log.debug(""); 34 | this.broadcastBean = broadcastBean; 35 | this.broadcastConfig = broadcastBean.getBroadcastConfig(); 36 | this.xmlFile = broadcastConfig.getConfigFile(); 37 | try { 38 | InputStream is = JGroupsPubSub.class.getClassLoader() 39 | .getResourceAsStream(this.xmlFile); 40 | 41 | channel = new JChannel(is); 42 | channel.setReceiver(this); 43 | } catch (Exception e) { 44 | e.printStackTrace(); 45 | } 46 | log.debug(""); 47 | 48 | } 49 | 50 | public void pub(String channel, byte[] message) { 51 | Message msg = new Message(null, null, message); 52 | try { 53 | this.channel.send(msg); 54 | } catch (Exception e) { 55 | e.printStackTrace(); 56 | } 57 | } 58 | 59 | public void close() { 60 | channel.close(); 61 | } 62 | 63 | /** 64 | * 组中成员变化时 65 | * 66 | * @param view group view 67 | */ 68 | public void viewAccepted(View view) { 69 | StringBuffer sb = new StringBuffer("Group Members Changed, LIST: "); 70 | List
addrs = view.getMembers(); 71 | for (int i = 0; i < addrs.size(); i++) { 72 | if (i > 0) 73 | sb.append(','); 74 | sb.append(addrs.get(i).toString()); 75 | } 76 | } 77 | 78 | @SuppressWarnings("rawtypes") 79 | public void receive(Message msg) { 80 | //无效消息 81 | byte[] buffers = msg.getBuffer(); 82 | if (buffers.length < 1) { 83 | System.out.println("Message is empty."); 84 | return; 85 | } 86 | //不处理发送给自己的消息 87 | if (msg.getSrc().equals(channel.getAddress())) 88 | return; 89 | 90 | try { 91 | Command cmd = Command.parse(buffers); 92 | if (cmd == null) 93 | return; 94 | 95 | Cache cache = AtomsContext.getCache(cmd.getRegion(), 1); 96 | switch (cmd.getOperator()) { 97 | case Command.OPT_DELETE_KEY: 98 | Object key = cmd.getKey(); 99 | if (key instanceof List) { 100 | cache.evict((List) key,false); 101 | } else { 102 | cache.evict(cmd.getKey(),false); 103 | } 104 | break; 105 | case Command.OPT_CLEAR_KEY: 106 | cache.clear(false); 107 | break; 108 | case Command.OPT_PUT_KEY: 109 | log.debug("on Message[put]["+cmd.getRegion()+"]["+cmd.getKey()+"]"); 110 | if(broadsetConfig==null||(broadsetConfig!=null&&broadsetConfig.contains(cmd.getBroadsetKey()))){ 111 | if(cmd.getExpiretime()>0){ 112 | cache.put(cmd.getKey(), cmd.getVal(),cmd.getExpiretime(), false); 113 | }else { 114 | cache.put(cmd.getKey(), cmd.getVal(), false); 115 | } 116 | } 117 | break; 118 | default: 119 | } 120 | } catch (Exception e) { 121 | e.printStackTrace(); 122 | } 123 | } 124 | } 125 | -------------------------------------------------------------------------------- /atoms-core/src/main/java/com/lovver/atoms/core/CacheManager.java: -------------------------------------------------------------------------------- 1 | package com.lovver.atoms.core; 2 | 3 | import java.util.List; 4 | import java.util.Map; 5 | 6 | import com.lovver.atoms.cache.Cache; 7 | import com.lovver.atoms.cache.CacheEventListener; 8 | import com.lovver.atoms.cache.CacheEventListenerFactory; 9 | import com.lovver.atoms.cache.CacheProvider; 10 | import com.lovver.atoms.common.exception.CacheException; 11 | import com.lovver.atoms.context.AtomsContext; 12 | 13 | public class CacheManager { 14 | private static Map cacheProviders=AtomsContext.getCacheProvider(); 15 | 16 | public static Cache getCache(int level, String regionName, boolean autoCreate){ 17 | CacheProvider cacheProvider=cacheProviders.get(level+""); 18 | 19 | // if(level==cacheProviders.size()){ 20 | // //最后一级缓存不需要添加监听器 21 | // return cacheProvider.buildCache(regionName, autoCreate, null); 22 | // } 23 | CacheEventListener listener = null; 24 | if(level==1){ 25 | listener = CacheEventListenerFactory.getCacheEventListener(cacheProvider.name(),level); 26 | } 27 | return cacheProvider.buildCache(regionName, autoCreate, listener); 28 | } 29 | 30 | 31 | public static void shutdown(int level){ 32 | CacheProvider cacheProvider=cacheProviders.get(level+""); 33 | cacheProvider.stop(); 34 | } 35 | 36 | 37 | 38 | 39 | /** 40 | * 获取缓存中的数据 41 | * @param level Cache Level: L1 and L2 42 | * @param name Cache region name 43 | * @param key Cache key 44 | * @return Cache object 45 | */ 46 | public final static Object get(int level, String name, Object key){ 47 | //System.out.println("GET1 => " + name+":"+key); 48 | if(name!=null && key != null) { 49 | Cache cache = getCache(level, name, false); 50 | if (cache != null) 51 | return cache.get(key); 52 | } 53 | return null; 54 | } 55 | 56 | /** 57 | * 获取缓存中的数据 58 | * @param level Cache Level -> L1 and L2 59 | * @param resultClass Cache object class 60 | * @param name Cache region name 61 | * @param key Cache key 62 | * @return Cache object 63 | */ 64 | @SuppressWarnings("unchecked") 65 | public final static T get(int level, Class resultClass, String name, Object key){ 66 | //System.out.println("GET2 => " + name+":"+key); 67 | if(name!=null && key != null) { 68 | Cache cache =getCache(level, name, false); 69 | if (cache != null) 70 | return (T)cache.get(key); 71 | } 72 | return null; 73 | } 74 | 75 | /** 76 | * 写入缓存 77 | * @param level Cache Level: L1 and L2 78 | * @param name Cache region name 79 | * @param key Cache key 80 | * @param value Cache value 81 | */ 82 | public final static void set(int level, String name, Object key, Object value){ 83 | //System.out.println("SET => " + name+":"+key+"="+value); 84 | if(name!=null && key != null && value!=null) { 85 | Cache cache =getCache(level, name, true); 86 | if (cache != null) 87 | cache.put(key,value); 88 | } 89 | } 90 | 91 | /** 92 | * 清除缓存中的某个数据 93 | * @param level Cache Level: L1 and L2 94 | * @param name Cache region name 95 | * @param key Cache key 96 | */ 97 | public final static void evict(int level, String name, Object key){ 98 | //batchEvict(level, name, java.util.Arrays.asList(key)); 99 | if(name!=null && key != null) { 100 | Cache cache =getCache(level, name, false); 101 | if (cache != null) 102 | cache.evict(key); 103 | } 104 | } 105 | 106 | /** 107 | * 批量删除缓存中的一些数据 108 | * @param level Cache Level: L1 and L2 109 | * @param name Cache region name 110 | * @param keys Cache keys 111 | */ 112 | @SuppressWarnings("rawtypes") 113 | public final static void batchEvict(int level, String name, List keys) { 114 | if(name!=null && keys != null && keys.size() > 0) { 115 | Cache cache =getCache(level, name, false); 116 | if (cache != null) 117 | cache.evict(keys); 118 | } 119 | } 120 | 121 | /** 122 | * Clear the cache 123 | * @param level Cache level 124 | * @param name cache region name 125 | */ 126 | public final static void clear(int level, String name) throws CacheException { 127 | Cache cache =getCache(level, name, false); 128 | if(cache != null) 129 | cache.clear(); 130 | } 131 | 132 | /** 133 | * list cache keys 134 | * @param level Cache level 135 | * @param name cache region name 136 | * @return Key List 137 | */ 138 | @SuppressWarnings("rawtypes") 139 | public final static List keys(int level, String name) throws CacheException { 140 | Cache cache =getCache(level, name, false); 141 | return (cache!=null)?cache.keys():null; 142 | } 143 | } 144 | -------------------------------------------------------------------------------- /atoms-cache/atoms-cache-ehcache/src/main/java/com/lovver/atoms/cache/ehcache/EhCacheProvider.java: -------------------------------------------------------------------------------- 1 | package com.lovver.atoms.cache.ehcache; 2 | 3 | import java.net.URL; 4 | import java.util.List; 5 | import java.util.Map; 6 | import java.util.concurrent.ConcurrentHashMap; 7 | 8 | import com.lovver.atoms.cache.Cache; 9 | import com.lovver.atoms.config.AtomsCacheTTLConfigBean; 10 | import net.sf.ehcache.CacheManager; 11 | 12 | import net.sf.ehcache.Ehcache; 13 | import net.sf.ehcache.Element; 14 | import org.apache.commons.lang.StringUtils; 15 | import org.slf4j.Logger; 16 | import org.slf4j.LoggerFactory; 17 | 18 | import com.lovver.atoms.cache.CacheEventListener; 19 | import com.lovver.atoms.cache.CacheProvider; 20 | import com.lovver.atoms.common.annotation.SPI; 21 | import com.lovver.atoms.common.exception.CacheException; 22 | import com.lovver.atoms.common.utils.ClassUtils; 23 | import com.lovver.atoms.config.AtomsCacheBean; 24 | import com.lovver.atoms.context.AtomsContext; 25 | 26 | /** 27 | * EhCache Provider plugin 28 | * 29 | * @author jobell 30 | */ 31 | @SPI("ehcache") 32 | public class EhCacheProvider implements CacheProvider { 33 | 34 | private final static Logger log = LoggerFactory.getLogger(EhCacheProvider.class); 35 | 36 | private CacheManager manager; 37 | private ConcurrentHashMap _CacheManager = new ConcurrentHashMap () ; 38 | private int level; 39 | 40 | @Override 41 | public String name() { 42 | return "ehcache"; 43 | } 44 | 45 | 46 | 47 | public EhCache buildCache(String regionName, boolean autoCreate,final CacheEventListener listener) throws CacheException { 48 | // EhCache ehcache = null; 49 | EhCache ehcache = _CacheManager.get(regionName); 50 | if(ehcache == null && autoCreate){ 51 | try { 52 | synchronized(_CacheManager){ 53 | ehcache = _CacheManager.get(regionName); 54 | if(ehcache == null){ 55 | net.sf.ehcache.Cache cache = manager.getCache(regionName); 56 | if (cache == null) { 57 | log.warn("Could not find configuration [" + regionName + "]; using defaults."); 58 | 59 | Map mapTTL=AtomsContext.getTTLConfig(this.level); 60 | AtomsCacheTTLConfigBean ttl=mapTTL.get(regionName); 61 | if(ttl!=null&&StringUtils.isNotEmpty(ttl.getValue())){ 62 | cache=new net.sf.ehcache.Cache(regionName,1000,false,false,Long.parseLong(ttl.getValue()),Long.parseLong(ttl.getValue())); 63 | manager.addCache(cache); 64 | }else{ 65 | manager.addCache(regionName); 66 | cache = manager.getCache(regionName); 67 | } 68 | 69 | if(ttl!=null&&StringUtils.isNotEmpty(ttl.getValue())){ 70 | cache.getCacheConfiguration().setTimeToLiveSeconds(Long.parseLong(ttl.getValue())); 71 | } 72 | log.debug("started EHCache region: " + regionName); 73 | } 74 | ehcache = new EhCache(cache, listener,this.level); 75 | _CacheManager.put(regionName, ehcache); 76 | } 77 | } 78 | } 79 | catch (net.sf.ehcache.CacheException e) { 80 | throw new CacheException(e); 81 | } 82 | } 83 | return ehcache; 84 | } 85 | 86 | /** 87 | * Callback to perform any necessary initialization of the underlying cache implementation 88 | * during SessionFactory construction. 89 | * 90 | * @param cacheBean current configuration settings. 91 | */ 92 | public void start(AtomsCacheBean cacheBean) throws CacheException { 93 | if (manager != null) { 94 | log.warn("Attempt to restart an already started EhCacheProvider."); 95 | return; 96 | } 97 | String configFile=cacheBean.getCacheConfig().getConfigFile(); 98 | if(StringUtils.isNotEmpty(configFile)){ 99 | try{ 100 | manager=CacheManager.create(configFile); 101 | //manager = new CacheManager(configFile); 102 | }catch(Exception e){ 103 | URL uriConfigFile=ClassUtils.getDefaultClassLoader().getResource(configFile); 104 | // manager = new CacheManager(uriConfigFile); 105 | manager=CacheManager.create(uriConfigFile); 106 | } 107 | }else{ 108 | manager = CacheManager.getInstance(); 109 | } 110 | this.level=Integer.parseInt(cacheBean.getLevel()); 111 | // _CacheManager = new ConcurrentHashMap(); 112 | } 113 | 114 | /** 115 | * Callback to perform any necessary cleanup of the underlying cache implementation. 116 | */ 117 | public void stop() { 118 | if (manager != null) { 119 | manager.shutdown(); 120 | // _CacheManager.clear(); 121 | manager = null; 122 | } 123 | } 124 | 125 | } 126 | -------------------------------------------------------------------------------- /atoms-core/src/main/java/com/lovver/atoms/core/CacheChannel.java: -------------------------------------------------------------------------------- 1 | package com.lovver.atoms.core; 2 | 3 | import java.util.Collection; 4 | import java.util.List; 5 | import java.util.Map; 6 | 7 | import com.alibaba.fastjson.JSON; 8 | import com.lovver.atoms.cache.Cache; 9 | import com.lovver.atoms.cache.CacheProvider; 10 | import com.lovver.atoms.common.exception.CacheException; 11 | import com.lovver.atoms.config.AtomsCacheBean; 12 | import com.lovver.atoms.config.AtomsConfig; 13 | import com.lovver.atoms.context.AtomsContext; 14 | 15 | public class CacheChannel { 16 | private static CacheChannel instance=new CacheChannel(); 17 | 18 | private Map mCacheProvider=AtomsContext.getCacheProvider(); 19 | 20 | public static CacheChannel getInstance(){ 21 | return instance; 22 | } 23 | 24 | 25 | private Object[] getNextLevelCache(String region,Object key){ 26 | Object[] value=null; 27 | Map mCacheProvider=AtomsContext.getCacheProvider(); 28 | for(int i=2;i<=mCacheProvider.size();i++){ 29 | 30 | Cache cache=AtomsContext.getCache(region, i); 31 | value=new Object[]{ 32 | i,cache.get(key) 33 | }; 34 | if(value!=null){ 35 | break; 36 | } 37 | } 38 | 39 | return value; 40 | } 41 | 42 | 43 | /** 44 | * 获取缓存中的数据 45 | * 46 | * @param region 47 | * : Cache Region name 48 | * @param key 49 | * : Cache key 50 | * @return cache object 51 | */ 52 | public Object get(String region, Object key) { 53 | 54 | if (region != null && key != null) { 55 | Cache tCache=CacheManager.getCache(1, region, true); 56 | Object value=tCache.get(key); 57 | if(value==null){ 58 | Object[] levelValue=getNextLevelCache(region,key); 59 | if(levelValue!=null){ 60 | value=levelValue[1]; 61 | } 62 | tCache.put(key,value); 63 | } 64 | return value; 65 | } 66 | return null; 67 | } 68 | 69 | /** 70 | * 写入缓存 71 | * 72 | * @param region 73 | * : Cache Region name 74 | * @param key 75 | * : Cache key 76 | * @param value 77 | * : Cache value 78 | */ 79 | public void set(String region, Object key, Object value) { 80 | // System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa["+region+"]["+key+"]=="+ JSON.toJSONString(value)); 81 | if (region != null && key != null) { 82 | if (value == null) { 83 | evict(region, key); 84 | } else { 85 | for(int i=mCacheProvider.size();i>0;i--) { 86 | Cache cache = CacheManager.getCache(i, region, true); 87 | cache.evict(key); 88 | cache.put(key, value); 89 | } 90 | } 91 | } 92 | } 93 | 94 | public void set(String region, Object key, Object value,Integer expiretime) { 95 | if (region != null && key != null) { 96 | if (value == null) { 97 | evict(region, key); 98 | } else { 99 | for(int i=mCacheProvider.size();i>0;i--) { 100 | Cache cache = CacheManager.getCache(i, region, true); 101 | cache.evict(key); 102 | cache.put(key, value,expiretime); 103 | } 104 | } 105 | } 106 | } 107 | 108 | /** 109 | * 删除缓存 110 | * 111 | * @param region 112 | * : Cache Region name 113 | * @param key 114 | * : Cache key 115 | */ 116 | public void evict(String region, Object key) { 117 | for(int i=1;i<=mCacheProvider.size();i++) { 118 | Cache cache=CacheManager.getCache(i,region,true); 119 | cache.evict(key); 120 | } 121 | } 122 | 123 | /** 124 | * 批量删除缓存 125 | * 126 | * @param region 127 | * : Cache region name 128 | * @param keys 129 | * : Cache key 130 | */ 131 | @SuppressWarnings({ "rawtypes" }) 132 | public void batchEvict(String region, List keys) { 133 | for(int i=mCacheProvider.size();i>0;i--) { 134 | Cache cache = CacheManager.getCache(i, region, true); 135 | cache.evict(keys); 136 | } 137 | } 138 | 139 | /** 140 | * Clear the cache 141 | * 142 | * @param region 143 | * : Cache region name 144 | */ 145 | public void clear(String region) throws CacheException { 146 | for(int i=mCacheProvider.size();i>0;i--) { 147 | Cache cache = CacheManager.getCache(i, region, true); 148 | cache.clear(); 149 | } 150 | } 151 | 152 | /** 153 | * Get cache region keys 154 | * 155 | * @param region 156 | * : Cache region name 157 | * @return key list 158 | */ 159 | @SuppressWarnings("rawtypes") 160 | public List keys(String region) throws CacheException { 161 | List lstRet=null; 162 | Cache cache = CacheManager.getCache(1, region, true); 163 | lstRet = cache.keys(); 164 | return lstRet; 165 | } 166 | 167 | 168 | 169 | /** 170 | * 关闭到通道的连接 171 | */ 172 | public void close() { 173 | Collection lstCacheBean=AtomsConfig.getAtomsConfig().getCache(); 174 | for(AtomsCacheBean cp:lstCacheBean){ 175 | CacheManager.shutdown(Integer.parseInt(cp.getLevel())); 176 | } 177 | 178 | } 179 | } -------------------------------------------------------------------------------- /atoms-serializer/atoms-serializer-kryopool/src/main/java/com/lovver/atoms/serializer/kryopool/KryoPoolSerializer.java: -------------------------------------------------------------------------------- 1 | package com.lovver.atoms.serializer.kryopool; 2 | 3 | import java.io.IOException; 4 | import java.util.Deque; 5 | import java.util.concurrent.ConcurrentLinkedDeque; 6 | 7 | import com.esotericsoftware.kryo.Kryo; 8 | import com.esotericsoftware.kryo.io.Input; 9 | import com.esotericsoftware.kryo.io.Output; 10 | import com.lovver.atoms.common.annotation.SPI; 11 | import com.lovver.atoms.common.exception.CacheException; 12 | import com.lovver.atoms.serializer.Serializer; 13 | 14 | @SPI("kryopool") 15 | public class KryoPoolSerializer implements Serializer{ 16 | 17 | /** 18 | * Kryo 的包装 19 | */ 20 | private static class KryoHolder { 21 | private Kryo kryo; 22 | static final int BUFFER_SIZE = 1024; 23 | private Output output = new Output(BUFFER_SIZE, -1); //reuse 24 | private Input input = new Input(); 25 | 26 | KryoHolder(Kryo kryo) { 27 | this.kryo = kryo; 28 | } 29 | 30 | } 31 | 32 | 33 | interface KryoPool { 34 | 35 | /** 36 | * get o kryo object 37 | * @return KryoHolder instance 38 | */ 39 | KryoHolder get(); 40 | 41 | /** 42 | * return object 43 | * @param kryo holder 44 | */ 45 | void offer(KryoHolder kryo); 46 | } 47 | 48 | 49 | 50 | /** 51 | * 由于kryo创建的代价相对较高 ,这里使用空间换时间 52 | * 对KryoHolder对象进行重用 53 | */ 54 | public static class KryoPoolImpl implements KryoPool { 55 | /** 56 | * default is 1500 57 | * online server limit 3K 58 | */ 59 | 60 | /** 61 | * thread safe list 62 | */ 63 | private final Deque kryoHolderDeque=new ConcurrentLinkedDeque(); 64 | 65 | private KryoPoolImpl() { 66 | 67 | } 68 | 69 | /** 70 | * @return KryoPool instance 71 | */ 72 | public static KryoPool getInstance() { 73 | return Singleton.pool; 74 | } 75 | 76 | /** 77 | * get o KryoHolder object 78 | * 79 | * @return KryoHolder instance 80 | */ 81 | @Override 82 | public KryoHolder get() { 83 | KryoHolder kryoHolder = kryoHolderDeque.pollFirst(); // Retrieves and removes the head of the queue represented by this table 84 | return kryoHolder == null ? creatInstnce() : kryoHolder; 85 | } 86 | 87 | /** 88 | * create a new kryo object to application use 89 | * @return KryoHolder instance 90 | */ 91 | public KryoHolder creatInstnce() { 92 | Kryo kryo = new Kryo(); 93 | kryo.setReferences(false);// 94 | return new KryoHolder(kryo); 95 | } 96 | 97 | /** 98 | * return object 99 | * Inserts the specified element at the tail of this queue. 100 | * 101 | * @param kryoHolder ... 102 | */ 103 | @Override 104 | public void offer(KryoHolder kryoHolder) { 105 | kryoHolderDeque.addLast(kryoHolder); 106 | } 107 | 108 | /** 109 | * creat a Singleton 110 | */ 111 | private static class Singleton { 112 | private static final KryoPool pool = new KryoPoolImpl(); 113 | } 114 | } 115 | 116 | @Override 117 | public String name() { 118 | return "kryo_pool_ser"; 119 | } 120 | 121 | /** 122 | * Serialize object 123 | * @param obj what to serialize 124 | * @return return serialize data 125 | */ 126 | @Override 127 | public byte[] serialize(Object obj) throws IOException { 128 | KryoHolder kryoHolder = null; 129 | if (obj == null) throw new CacheException("obj can not be null"); 130 | try { 131 | kryoHolder = KryoPoolImpl.getInstance().get(); 132 | kryoHolder.output.clear(); //clear Output -->每次调用的时候 重置 133 | kryoHolder.kryo.writeClassAndObject(kryoHolder.output, obj); 134 | return kryoHolder.output.toBytes();// 无法避免拷贝 ~~~ 135 | } catch (CacheException e) { 136 | throw new CacheException("Serialize obj exception"); 137 | } finally { 138 | KryoPoolImpl.getInstance().offer(kryoHolder); 139 | obj = null; //GC 140 | } 141 | } 142 | 143 | /** 144 | * Deserialize data 145 | * @param bytes what to deserialize 146 | * @return object 147 | */ 148 | @Override 149 | public Object deserialize(byte[] bytes) throws IOException { 150 | KryoHolder kryoHolder = null; 151 | if (bytes == null) throw new CacheException("bytes can not be null"); 152 | try { 153 | kryoHolder = KryoPoolImpl.getInstance().get(); 154 | kryoHolder.input.setBuffer(bytes, 0, bytes.length);//call it ,and then use input object ,discard any array 155 | return kryoHolder.kryo.readClassAndObject(kryoHolder.input); 156 | } catch (CacheException e) { 157 | throw new CacheException("Deserialize bytes exception"); 158 | } finally { 159 | KryoPoolImpl.getInstance().offer(kryoHolder); 160 | bytes = null; // for gc 161 | } 162 | } 163 | } 164 | -------------------------------------------------------------------------------- /atoms-config/src/main/java/com/lovver/atoms/config/AtomsCacheConfigBean.java: -------------------------------------------------------------------------------- 1 | package com.lovver.atoms.config; 2 | 3 | import com.thoughtworks.xstream.annotations.XStreamAlias; 4 | import com.thoughtworks.xstream.annotations.XStreamAsAttribute; 5 | 6 | @XStreamAlias("cacheConfig") 7 | public class AtomsCacheConfigBean { 8 | 9 | @XStreamAsAttribute 10 | private String configFile; 11 | 12 | @XStreamAsAttribute 13 | private String host; 14 | 15 | @XStreamAsAttribute 16 | private String port; 17 | 18 | @XStreamAsAttribute 19 | private String timeout; 20 | 21 | @XStreamAsAttribute 22 | private String password; 23 | 24 | @XStreamAsAttribute 25 | private String database; 26 | 27 | @XStreamAsAttribute 28 | private String namespace; 29 | 30 | @XStreamAsAttribute 31 | private String maxTotal; 32 | 33 | @XStreamAsAttribute 34 | private String maxIdle; 35 | 36 | @XStreamAsAttribute 37 | private String maxWaitMillis; 38 | 39 | @XStreamAsAttribute 40 | private String minEvictableIdleTimeMillis; 41 | 42 | @XStreamAsAttribute 43 | private String minIdle; 44 | 45 | @XStreamAsAttribute 46 | private String numTestsPerEvictionRun; 47 | 48 | @XStreamAsAttribute 49 | private String lifo; 50 | 51 | @XStreamAsAttribute 52 | private String softMinEvictableIdleTimeMillis; 53 | 54 | @XStreamAsAttribute 55 | private String testOnBorrow; 56 | 57 | @XStreamAsAttribute 58 | private String testOnReturn; 59 | 60 | @XStreamAsAttribute 61 | private String testWhileIdle; 62 | 63 | @XStreamAsAttribute 64 | private String timeBetweenEvictionRunsMillis; 65 | 66 | @XStreamAsAttribute 67 | private String blockWhenExhausted; 68 | 69 | public String getConfigFile() { 70 | return configFile; 71 | } 72 | 73 | public void setConfigFile(String configFile) { 74 | this.configFile = configFile; 75 | } 76 | 77 | public String getHost() { 78 | return host; 79 | } 80 | 81 | public void setHost(String host) { 82 | this.host = host; 83 | } 84 | 85 | public String getPort() { 86 | return port; 87 | } 88 | 89 | public void setPort(String port) { 90 | this.port = port; 91 | } 92 | 93 | public String getTimeout() { 94 | return timeout; 95 | } 96 | 97 | public void setTimeout(String timeout) { 98 | this.timeout = timeout; 99 | } 100 | 101 | public String getPassword() { 102 | return password; 103 | } 104 | 105 | public void setPassword(String password) { 106 | this.password = password; 107 | } 108 | 109 | public String getDatabase() { 110 | return database; 111 | } 112 | 113 | public void setDatabase(String database) { 114 | this.database = database; 115 | } 116 | 117 | public String getNamespace() { 118 | return namespace; 119 | } 120 | 121 | public void setNamespace(String namespace) { 122 | this.namespace = namespace; 123 | } 124 | 125 | public String getMaxTotal() { 126 | return maxTotal; 127 | } 128 | 129 | public void setMaxTotal(String maxTotal) { 130 | this.maxTotal = maxTotal; 131 | } 132 | 133 | public String getMaxIdle() { 134 | return maxIdle; 135 | } 136 | 137 | public void setMaxIdle(String maxIdle) { 138 | this.maxIdle = maxIdle; 139 | } 140 | 141 | public String getMaxWaitMillis() { 142 | return maxWaitMillis; 143 | } 144 | 145 | public void setMaxWaitMillis(String maxWaitMillis) { 146 | this.maxWaitMillis = maxWaitMillis; 147 | } 148 | 149 | public String getMinEvictableIdleTimeMillis() { 150 | return minEvictableIdleTimeMillis; 151 | } 152 | 153 | public void setMinEvictableIdleTimeMillis(String minEvictableIdleTimeMillis) { 154 | this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis; 155 | } 156 | 157 | public String getMinIdle() { 158 | return minIdle; 159 | } 160 | 161 | public void setMinIdle(String minIdle) { 162 | this.minIdle = minIdle; 163 | } 164 | 165 | public String getNumTestsPerEvictionRun() { 166 | return numTestsPerEvictionRun; 167 | } 168 | 169 | public void setNumTestsPerEvictionRun(String numTestsPerEvictionRun) { 170 | this.numTestsPerEvictionRun = numTestsPerEvictionRun; 171 | } 172 | 173 | public String getLifo() { 174 | return lifo; 175 | } 176 | 177 | public void setLifo(String lifo) { 178 | this.lifo = lifo; 179 | } 180 | 181 | public String getSoftMinEvictableIdleTimeMillis() { 182 | return softMinEvictableIdleTimeMillis; 183 | } 184 | 185 | public void setSoftMinEvictableIdleTimeMillis( 186 | String softMinEvictableIdleTimeMillis) { 187 | this.softMinEvictableIdleTimeMillis = softMinEvictableIdleTimeMillis; 188 | } 189 | 190 | public String getTestOnBorrow() { 191 | return testOnBorrow; 192 | } 193 | 194 | public void setTestOnBorrow(String testOnBorrow) { 195 | this.testOnBorrow = testOnBorrow; 196 | } 197 | 198 | public String getTestOnReturn() { 199 | return testOnReturn; 200 | } 201 | 202 | public void setTestOnReturn(String testOnReturn) { 203 | this.testOnReturn = testOnReturn; 204 | } 205 | 206 | public String getTestWhileIdle() { 207 | return testWhileIdle; 208 | } 209 | 210 | public void setTestWhileIdle(String testWhileIdle) { 211 | this.testWhileIdle = testWhileIdle; 212 | } 213 | 214 | public String getTimeBetweenEvictionRunsMillis() { 215 | return timeBetweenEvictionRunsMillis; 216 | } 217 | 218 | public void setTimeBetweenEvictionRunsMillis( 219 | String timeBetweenEvictionRunsMillis) { 220 | this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis; 221 | } 222 | 223 | public String getBlockWhenExhausted() { 224 | return blockWhenExhausted; 225 | } 226 | 227 | public void setBlockWhenExhausted(String blockWhenExhausted) { 228 | this.blockWhenExhausted = blockWhenExhausted; 229 | } 230 | 231 | } 232 | -------------------------------------------------------------------------------- /atoms-context/src/main/java/com/lovver/atoms/context/AtomsContext.java: -------------------------------------------------------------------------------- 1 | package com.lovver.atoms.context; 2 | 3 | import java.util.List; 4 | import java.util.Map; 5 | import java.util.UUID; 6 | import java.util.concurrent.ConcurrentHashMap; 7 | import java.util.concurrent.CopyOnWriteArraySet; 8 | 9 | import com.lovver.atoms.broadcast.BroadCast; 10 | import com.lovver.atoms.broadcast.BroadCastFactory; 11 | import com.lovver.atoms.cache.Cache; 12 | import com.lovver.atoms.cache.CacheEventListener; 13 | import com.lovver.atoms.cache.CacheEventListenerFactory; 14 | import com.lovver.atoms.cache.CacheProvider; 15 | import com.lovver.atoms.cache.CacheProviderFactory; 16 | import com.lovver.atoms.common.utils.StringUtils; 17 | import com.lovver.atoms.config.*; 18 | import com.lovver.atoms.serializer.Serializer; 19 | import com.lovver.atoms.serializer.SerializerFactory; 20 | 21 | public class AtomsContext { 22 | private static AtomsBean atomBean = AtomsConfig.getAtomsConfig(); 23 | private static Serializer serializer = null; 24 | private static BroadCast broadCast = null; 25 | 26 | 27 | private static ConcurrentHashMap cacheProvider=new ConcurrentHashMap(); 28 | private static ConcurrentHashMap cacheConfig=new ConcurrentHashMap(); 29 | // private static ConcurrentHashMap broadCasts=new ConcurrentHashMap(); 30 | private static ConcurrentHashMap> cahcelevelTTLConfig=new ConcurrentHashMap>(); 31 | private static CopyOnWriteArraySet broadsetConfig = new CopyOnWriteArraySet(); 32 | static { 33 | try { 34 | serializer=SerializerFactory.getSerializer(atomBean.getSerializer()); 35 | List lstCache= atomBean.getCache(); 36 | AtomsBroadCastBean broadCastBean=atomBean.getBroadcast(); 37 | if(broadCastBean!=null) { 38 | List lstBroadSetConfig = broadCastBean.getLstBroadset(); 39 | if (lstBroadSetConfig != null) { 40 | for (AtomsBroadsetBean broadset : lstBroadSetConfig) { 41 | broadsetConfig.add(broadset.getRegion() + "_" + broadset.getKey()); 42 | } 43 | } 44 | broadCast=BroadCastFactory.getBroadCast(atomBean.getBroadcast()); 45 | } 46 | 47 | 48 | 49 | ConcurrentHashMap levelTTLConfig=null; 50 | for(AtomsCacheBean cacheBean:lstCache){ 51 | levelTTLConfig=new ConcurrentHashMap(); 52 | 53 | if(StringUtils.isEmpty(cacheBean.getLevel())){ 54 | throw new RuntimeException("cache level must give!"); 55 | } 56 | int level=Integer.parseInt(cacheBean.getLevel()); 57 | cacheProvider.put(cacheBean.getLevel(), CacheProviderFactory.getCacheProvider(cacheBean,level)); 58 | 59 | cacheConfig.put(cacheBean.getLevel(), cacheBean); 60 | 61 | AtomsCacheTTLBean cacheTTLBean=cacheBean.getCacheTTL(); 62 | if(null!=cacheTTLBean){ 63 | List lstTTLConfigBean=cacheTTLBean.getLstTTL(); 64 | if(null!=lstTTLConfigBean&&lstTTLConfigBean.size()>0){ 65 | for(AtomsCacheTTLConfigBean ttl:lstTTLConfigBean){ 66 | levelTTLConfig.put(ttl.getName(), ttl); 67 | } 68 | } 69 | } 70 | cahcelevelTTLConfig.put(cacheBean.getLevel(), levelTTLConfig); 71 | } 72 | 73 | } catch (InstantiationException e) { 74 | e.printStackTrace(); 75 | } catch (IllegalAccessException e) { 76 | e.printStackTrace(); 77 | } 78 | } 79 | public static AtomsBroadCastBean getAtomsBroadCastBean(){ 80 | return atomBean.getBroadcast(); 81 | } 82 | 83 | public static AtomsCacheBean getAtomsCacheBean(int level){ 84 | return cacheConfig.get(level+""); 85 | } 86 | 87 | public static Map getCacheProvider(){ 88 | return cacheProvider; 89 | } 90 | 91 | public static Serializer getSerializer(){ 92 | return serializer; 93 | } 94 | 95 | public static BroadCast getBroadCast(){ 96 | return broadCast; 97 | } 98 | 99 | public static Cache getCache(String region,int level){ 100 | CacheProvider cacheProvider=AtomsContext.getCacheProvider().get(level+""); 101 | CacheEventListener listener=null; 102 | if(level==1){//只有第一级缓存有监听器 103 | listener=CacheEventListenerFactory.getCacheEventListener(cacheProvider.name(),level); 104 | } 105 | Cache cache=cacheProvider.buildCache(region, true, listener); 106 | return cache; 107 | } 108 | 109 | 110 | public static Map getTTLConfig(int level){ 111 | return cahcelevelTTLConfig.get(level+""); 112 | } 113 | /** 114 | * 115 | * @param region 116 | * @param level 117 | * @return Object[]{cache,listener} 118 | */ 119 | public static Object[] getCacheAndListener(String region,int level){ 120 | CacheProvider cacheProvider=AtomsContext.getCacheProvider().get(level+""); 121 | CacheEventListener listener=null; 122 | if(level==1){ 123 | listener=CacheEventListenerFactory.getCacheEventListener(cacheProvider.name(),level); 124 | } 125 | Cache cache=cacheProvider.buildCache(region, true, listener); 126 | return new Object[]{cache,listener}; 127 | } 128 | 129 | public static String getApplicationName(){ 130 | return atomBean.getApplication(); 131 | } 132 | 133 | 134 | public static CopyOnWriteArraySet getBroadsetConfig(){ 135 | if(broadsetConfig==null||broadsetConfig.size()==0){ 136 | return null; 137 | } 138 | return broadsetConfig; 139 | } 140 | } 141 | -------------------------------------------------------------------------------- /atoms-cache/atoms-cache-redis/src/main/java/com/lovver/atoms/cache/redis/RedisCacheProvider.java: -------------------------------------------------------------------------------- 1 | package com.lovver.atoms.cache.redis; 2 | 3 | 4 | import java.util.Map; 5 | import java.util.concurrent.ConcurrentHashMap; 6 | 7 | import com.lovver.atoms.config.AtomsCacheTTLConfigBean; 8 | import org.apache.commons.lang.StringUtils; 9 | 10 | import redis.clients.jedis.Jedis; 11 | import redis.clients.jedis.JedisPool; 12 | import redis.clients.jedis.JedisPoolConfig; 13 | 14 | import com.lovver.atoms.cache.Cache; 15 | import com.lovver.atoms.cache.CacheEventListener; 16 | import com.lovver.atoms.cache.CacheProvider; 17 | import com.lovver.atoms.common.annotation.SPI; 18 | import com.lovver.atoms.common.exception.CacheException; 19 | import com.lovver.atoms.config.AtomsCacheBean; 20 | import com.lovver.atoms.config.AtomsCacheConfigBean; 21 | import com.lovver.atoms.context.AtomsContext; 22 | 23 | /** 24 | * Redis 缓存实现 25 | * @author Winter Lau 26 | * @author wendal 27 | */ 28 | @SPI("redis") 29 | public class RedisCacheProvider implements CacheProvider { 30 | 31 | private JedisPool pool; 32 | private AtomsCacheConfigBean cacheConfig; 33 | private String host; 34 | private int level; 35 | 36 | protected ConcurrentHashMap caches = new ConcurrentHashMap<>(); 37 | 38 | public String name() { 39 | return "redis"; 40 | } 41 | 42 | // 这个实现有个问题,如果不使用RedisCacheProvider,但又使用RedisCacheChannel,这就NPE了 43 | public Jedis getResource() { 44 | return pool.getResource(); 45 | } 46 | 47 | @Override 48 | public Cache buildCache(String regionName, boolean autoCreate, CacheEventListener listener) throws CacheException { 49 | // 虽然这个实现在并发时有概率出现同一各regionName返回不同的实例 50 | // 但返回的实例一次性使用,所以加锁了并没有增加收益 51 | RedisCache cache = caches.get(regionName); 52 | if (cache == null) { 53 | synchronized (caches) { 54 | Map mapTTL=AtomsContext.getTTLConfig(this.level); 55 | AtomsCacheTTLConfigBean ttlConfigBean=mapTTL.get(regionName); 56 | 57 | cache = new RedisCache(regionName, pool,cacheConfig.getNamespace(),listener,host,this.level); 58 | caches.put(regionName, cache); 59 | } 60 | } 61 | return cache; 62 | } 63 | 64 | private String null2default(String value,String defalutValue){ 65 | if(StringUtils.isEmpty(value)){ 66 | return defalutValue; 67 | }else{ 68 | return value; 69 | } 70 | } 71 | 72 | @Override 73 | public void start(AtomsCacheBean cacheBean) throws CacheException { 74 | JedisPoolConfig config = new JedisPoolConfig(); 75 | cacheConfig=cacheBean.getCacheConfig(); 76 | 77 | String host = null2default(cacheConfig.getHost(),"127.0.0.1"); 78 | this.host=host; 79 | String password = cacheConfig.getPassword(); 80 | if(StringUtils.isEmpty(password)){ 81 | password=null; 82 | } 83 | String sPort=null2default(cacheConfig.getPort(),"6379"); 84 | int port = Integer.parseInt(sPort); 85 | 86 | String sTimeout=null2default(cacheConfig.getTimeout(),"2000"); 87 | int timeout = Integer.parseInt(sTimeout);//getProperty(props, "timeout", 2000); 88 | 89 | String sDatabase=null2default(cacheConfig.getDatabase(),"0"); 90 | int database =Integer.parseInt(sDatabase); 91 | 92 | String sBlockWhenExhausted=null2default(cacheConfig.getBlockWhenExhausted(),"true"); 93 | config.setBlockWhenExhausted(Boolean.parseBoolean(sBlockWhenExhausted)); 94 | 95 | String sMaxIdle=null2default(cacheConfig.getMaxIdle(),"10"); 96 | config.setMaxIdle(Integer.parseInt(sMaxIdle)); 97 | 98 | String sMinIdle=null2default(cacheConfig.getMinIdle(),"5"); 99 | config.setMinIdle(Integer.parseInt(sMinIdle)); 100 | 101 | String sMaxTotal=null2default(cacheConfig.getMaxTotal(), "10000"); 102 | config.setMaxTotal(Integer.parseInt(sMaxTotal)); 103 | 104 | String sMaxWait=null2default(cacheConfig.getMaxWaitMillis(), "100"); 105 | config.setMaxWaitMillis(Integer.parseInt(sMaxWait)); 106 | 107 | String sTestWhileIdle=null2default(cacheConfig.getTestWhileIdle(), "false"); 108 | config.setTestWhileIdle(Boolean.parseBoolean(sTestWhileIdle)); 109 | 110 | String sTestOnBorrow=null2default(cacheConfig.getTestOnBorrow(), "true"); 111 | config.setTestOnBorrow(Boolean.parseBoolean(sTestOnBorrow)); 112 | 113 | String sTestOnReturn =null2default(cacheConfig.getTestOnReturn(), "false"); 114 | config.setTestOnReturn(Boolean.parseBoolean(sTestOnReturn)); 115 | 116 | String sNumTestsPerEvictionRun=null2default(cacheConfig.getNumTestsPerEvictionRun(), "10"); 117 | config.setNumTestsPerEvictionRun(Integer.parseInt(sNumTestsPerEvictionRun)); 118 | 119 | String sMinEvictableIdelTimeMillis=null2default(cacheConfig.getMinEvictableIdleTimeMillis(), "1000"); 120 | config.setMinEvictableIdleTimeMillis(Integer.parseInt(sMinEvictableIdelTimeMillis)); 121 | 122 | String sSoftMinEvictableIdleTimeMillis=null2default(cacheConfig.getSoftMinEvictableIdleTimeMillis(), "10"); 123 | config.setSoftMinEvictableIdleTimeMillis(Integer.parseInt(sSoftMinEvictableIdleTimeMillis)); 124 | 125 | String timeBetweenEvictionRunsMillis=null2default(cacheConfig.getTimeBetweenEvictionRunsMillis(), "10"); 126 | config.setTimeBetweenEvictionRunsMillis(Integer.parseInt(timeBetweenEvictionRunsMillis)); 127 | 128 | String lifo=null2default(cacheConfig.getLifo(), "false"); 129 | config.setLifo(Boolean.parseBoolean(lifo)); 130 | pool = new JedisPool(config, host, port, timeout, password, database); 131 | 132 | level=Integer.parseInt(cacheBean.getLevel()); 133 | } 134 | 135 | @Override 136 | public void stop() { 137 | pool.destroy(); 138 | caches.clear(); 139 | } 140 | } 141 | -------------------------------------------------------------------------------- /atoms-config/src/test/resources/applicationContext.xml: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | classpath:/atoms.properties 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | -------------------------------------------------------------------------------- /atoms-common/src/main/java/com/lovver/atoms/common/utils/ReflectionUtils.java: -------------------------------------------------------------------------------- 1 | package com.lovver.atoms.common.utils; 2 | 3 | import java.lang.reflect.*; 4 | 5 | import org.slf4j.Logger; 6 | import org.slf4j.LoggerFactory; 7 | 8 | /** 9 | * 反射的Utils函数集合. 提供访问私有变量,获取泛型类型Class,提取集合中元素的属性等Utils函数. 10 | * 11 | * @author lei 12 | */ 13 | public class ReflectionUtils { 14 | 15 | private static Logger logger = LoggerFactory.getLogger(ReflectionUtils.class); 16 | 17 | private ReflectionUtils() { 18 | } 19 | 20 | /** 21 | * 直接读取对象属性值,无视private/protected修饰符,不经过getter函数. 22 | */ 23 | public static Object getFieldValue(final Object object, final String fieldName) { 24 | Field field = getDeclaredField(object, fieldName); 25 | 26 | if (field == null) 27 | throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + object + "]"); 28 | 29 | makeAccessible(field); 30 | 31 | Object result = null; 32 | try { 33 | result = field.get(object); 34 | } catch (IllegalAccessException e) { 35 | logger.error("不可能抛出的异常{}", e.getMessage()); 36 | } 37 | return result; 38 | } 39 | 40 | /** 41 | * 直接设置对象属性值,无视private/protected修饰符,不经过setter函数. 42 | */ 43 | public static void setFieldValue(final Object object, final String fieldName, final Object value) { 44 | Field field = getDeclaredField(object, fieldName); 45 | 46 | if (field == null) 47 | throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + object + "]"); 48 | 49 | makeAccessible(field); 50 | 51 | try { 52 | field.set(object, value); 53 | } catch (IllegalAccessException e) { 54 | logger.error("不可能抛出的异常:{}", e.getMessage()); 55 | } 56 | } 57 | 58 | /** 59 | * 循环向上转型,获取对象的DeclaredField. 60 | */ 61 | protected static Field getDeclaredField(final Object object, final String fieldName) { 62 | assert(object!=null); 63 | return getDeclaredField(object.getClass(), fieldName); 64 | } 65 | 66 | /** 67 | * 循环向上转型,获取类的DeclaredField. 68 | */ 69 | @SuppressWarnings("unchecked") 70 | protected static Field getDeclaredField(final Class clazz, final String fieldName) { 71 | for (Class superClass = clazz; superClass != Object.class; superClass = superClass.getSuperclass()) { 72 | try { 73 | return superClass.getDeclaredField(fieldName); 74 | } catch (NoSuchFieldException e) { 75 | // Field不在当前类定义,继续向上转型 76 | } 77 | } 78 | return null; 79 | } 80 | 81 | /** 82 | * 强制转换fileld可访问. 83 | */ 84 | protected static void makeAccessible(final Field field) { 85 | if (!Modifier.isPublic(field.getModifiers()) || !Modifier.isPublic(field.getDeclaringClass().getModifiers())) { 86 | field.setAccessible(true); 87 | } 88 | } 89 | 90 | /** 91 | * 通过反射,获得定义Class时声明的父类的泛型参数的类型. 如public UserDao extends HibernateDao 92 | * 93 | * @param clazz 94 | * The class to introspect 95 | * @return the first generic declaration, or Object.class if cannot be 96 | * determined 97 | */ 98 | @SuppressWarnings("unchecked") 99 | public static Class getSuperClassGenricType(final Class clazz) { 100 | return getSuperClassGenricType(clazz, 0); 101 | } 102 | 103 | /** 104 | * 通过反射,获得定义Class时声明的父类的泛型参数的类型. 如public UserDao extends 105 | * HibernateDao 106 | * 107 | * @param clazz 108 | * clazz The class to introspect 109 | * @param index 110 | * the Index of the generic ddeclaration,start from 0. 111 | * @return the index generic declaration, or Object.class if cannot be 112 | * determined 113 | */ 114 | 115 | @SuppressWarnings("unchecked") 116 | public static Class getSuperClassGenricType(final Class clazz, final int index) { 117 | 118 | Type genType = clazz.getGenericSuperclass(); 119 | 120 | if (!(genType instanceof ParameterizedType)) { 121 | logger.warn(clazz.getSimpleName() + "'s superclass not ParameterizedType"); 122 | return Object.class; 123 | } 124 | 125 | Type[] params = ((ParameterizedType) genType).getActualTypeArguments(); 126 | 127 | if (index >= params.length || index < 0) { 128 | logger.warn("Index: " + index + ", Size of " + clazz.getSimpleName() + "'s Parameterized Type: " + params.length); 129 | return Object.class; 130 | } 131 | if (!(params[index] instanceof Class)) { 132 | logger.warn(clazz.getSimpleName() + " not set the actual class on superclass generic parameter"); 133 | return Object.class; 134 | } 135 | return (Class) params[index]; 136 | } 137 | 138 | public static T invokeStaticMethod(String className, String methodName) { 139 | 140 | Class threadClazz = null; 141 | Method method = null; 142 | try { 143 | threadClazz = Class.forName(className); 144 | method = threadClazz.getMethod(methodName); 145 | return (T)method.invoke(null); 146 | } catch (IllegalAccessException e) { 147 | e.printStackTrace(); 148 | } catch (InvocationTargetException e) { 149 | e.printStackTrace(); 150 | } catch (ClassNotFoundException e) { 151 | e.printStackTrace(); 152 | } catch (NoSuchMethodException e) { 153 | e.printStackTrace(); 154 | } 155 | return null; 156 | } 157 | 158 | } --------------------------------------------------------------------------------