Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
xushaomin committed Apr 12, 2016
1 parent da204ff commit cfffb41
Show file tree
Hide file tree
Showing 4 changed files with 229 additions and 0 deletions.
43 changes: 43 additions & 0 deletions apple-id-redisson/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.appleframework.id</groupId>
<artifactId>apple-id</artifactId>
<version>0.0.2.RELEASE</version>
</parent>
<artifactId>apple-id-redisson</artifactId>
<name>apple-id-redisson</name>
<url>http://maven.apache.org</url>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.appleframework.id</groupId>
<artifactId>apple-id-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.8.0</version>
</dependency>

<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</dependency>

<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>2.2.11.APPLE</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package com.appleframework.id;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;

import org.apache.log4j.Logger;
import org.redisson.Config;
import org.redisson.Redisson;
import org.redisson.RedissonClient;
import org.redisson.core.RAtomicLong;


/**
* This id generator utilizes Redis (http://redis.io/) to generate serial IDs.
*
* <p>
* Persistency: IDs generated by this id-generator are persistent (assuming the
* Redis backend runs in persistent mode).
* </p>
*
* @author cruise.xu
* @since 0.1.0
*/
public class RedissonIdGenerator extends SerialIdGenerator {

private static Logger logger = Logger.getLogger(RedissonIdGenerator.class);

private RedissonClient redisson;

/**
* Helper method to obtain {@link RedissonIdGenerator}.
*
* @param redisHost
* @param redisPort
* @param redisUser
* @param redisPassword
* @param redisPoolConfig
* @return
*/
public static RedissonIdGenerator getInstance(final Config config) {
StringBuilder key = new StringBuilder();
key.append(config.toString());
try {
RedissonIdGenerator idGen = (RedissonIdGenerator) idGenerators.get(key.toString(),
new Callable<SerialIdGenerator>() {
@Override
public SerialIdGenerator call() throws Exception {
RedissonIdGenerator idGen = new RedissonIdGenerator();
RedissonClient redisson = Redisson.create(config);
idGen.setRedisson(redisson);
return idGen;
}
});
return idGen;
} catch (ExecutionException e) {
logger.warn(e.getMessage(), e);
return null;
}
}

/**
* {@inheritDoc}
*/
@Override
public RedissonIdGenerator init() {
Version.logVersion();
super.init();
return this;
}

public void destroy() {
try {
redisson.shutdown();
} catch (Exception e) {
logger.warn(e.getMessage(), e);
}
super.destroy();
}

/**
* {@inheritDoc}
*/
@Override
public long nextId(final String namespace) {
RAtomicLong ra = redisson.getAtomicLong(namespace);
return ra.incrementAndGet();
}

/**
* {@inheritDoc}
*/
@Override
public long currentId(final String namespace) {
RAtomicLong ra = redisson.getAtomicLong(namespace);
return ra.get();
}

/**
* {@inheritDoc}
*
* @since 0.4.0
*/
@Override
public boolean setValue(final String namespace, final long value) {
RAtomicLong ra = redisson.getAtomicLong(namespace);
ra.set(value);
return true;
}

public void setRedisson(RedissonClient redisson) {
this.redisson = redisson;
}

}
34 changes: 34 additions & 0 deletions apple-id-redisson/src/main/java/com/appleframework/id/Version.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.appleframework.id;

import java.net.URL;
import java.util.Enumeration;
import java.util.jar.Attributes;
import java.util.jar.Manifest;

import org.apache.log4j.Logger;

public class Version {

private static Logger logger = Logger.getLogger(Version.class);

public static void logVersion() {
try {
Enumeration<URL> resources = Version.class.getClassLoader().getResources("META-INF/MANIFEST.MF");
while (resources.hasMoreElements()) {
Manifest manifest = new Manifest(resources.nextElement().openStream());
Attributes attrs = manifest.getMainAttributes();
if (attrs == null) {
continue;
}
String name = attrs.getValue("Bundle-Name");
if (name != null && name.indexOf("apple-id") > -1) {
logger.info("Apple ID Redssion " + attrs.getValue("Bundle-Version"));
break;
}
}
} catch (Exception E) {
// skip it
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.appleframework.id.redisson;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
* Unit test for simple App.
*/
public class AppTest
extends TestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public AppTest( String testName )
{
super( testName );
}

/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( AppTest.class );
}

/**
* Rigourous Test :-)
*/
public void testApp()
{
assertTrue( true );
}
}

0 comments on commit cfffb41

Please sign in to comment.