提交 d63cfc8e 编写于 作者: R Rossen Stoyanchev

Add JdkIdGenerator and use it in SockJS client

Issue: SPR-12658
上级 7621cc78
......@@ -22,9 +22,11 @@ import java.util.Random;
import java.util.UUID;
/**
* A variation of {@link UUID#randomUUID()} that uses {@link SecureRandom} only for the
* initial seed and {@link Random} thereafter. This provides better performance in
* exchange for less securely random id's.
* An {@link org.springframework.util.IdGenerator IdGenerator} that uses
* {@link SecureRandom} for the initial seed and {@link Random} thereafter
* instead of calling {@link UUID#randomUUID()} every time as
* {@link org.springframework.util.JdkIdGenerator JdkIdGenerator} does.
* This provides a better balance between securely random id's and performance.
*
* @author Rossen Stoyanchev
* @author Rob Winch
......
/*
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.util;
import java.util.UUID;
/**
* An IdGenerator that calls {@link java.util.UUID#randomUUID()}.
*
* @author Rossen Stoyanchev
* @since 4.2
*/
public class JdkIdGenerator implements IdGenerator {
public UUID generateId() {
return UUID.randomUUID();
}
}
/*
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.util;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicLong;
/**
* An simple IdGenerator that starts at 1 and increments by 1 with each call.
*
* @author Rossen Stoyanchev
* @since 4.2
*/
public class SimpleIdGenerator implements IdGenerator {
private final AtomicLong mostSigBits = new AtomicLong(0);
private final AtomicLong leastSigBits = new AtomicLong(0);
@Override
public UUID generateId() {
long leastSigBits = this.leastSigBits.incrementAndGet();
if (leastSigBits == 0) {
this.mostSigBits.incrementAndGet();
}
return new UUID(this.mostSigBits.get(), leastSigBits);
}
}
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -19,8 +19,8 @@ package org.springframework.web.socket.sockjs.client;
import java.net.URI;
import java.util.UUID;
import org.springframework.util.AlternativeJdkIdGenerator;
import org.springframework.util.IdGenerator;
import org.springframework.util.JdkIdGenerator;
import org.springframework.web.socket.sockjs.transport.TransportType;
import org.springframework.web.util.UriComponentsBuilder;
......@@ -33,7 +33,7 @@ import org.springframework.web.util.UriComponentsBuilder;
*/
public class SockJsUrlInfo {
private static final IdGenerator idGenerator = new AlternativeJdkIdGenerator();
private static final IdGenerator idGenerator = new JdkIdGenerator();
private final URI sockJsUrl;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册