Skip to content

Instantly share code, notes, and snippets.

View nobodyiam's full-sized avatar
:electron:
Coding

Jason Song nobodyiam

:electron:
Coding
View GitHub Profile
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.jpa.EntityManagerFactoryAccessor;
import org.springframework.orm.jpa.EntityManagerFactoryUtils;
import org.springframework.orm.jpa.EntityManagerHolder;
import org.springframework.stereotype.Component;
import org.springframework.transaction.support.TransactionSynchronizationManager;
@Component
public class EntityManagerUtil extends EntityManagerFactoryAccessor {
@RestController
@RequestMapping("/notifications")
public class NotificationControllerSample implements MessageListener {
private static final long TIMEOUT = 360 * 60 * 1000;//6 hours
private final Multimap<String, DeferredResult<ResponseEntity<ApolloConfigNotification>>>
deferredResults = Multimaps.synchronizedSetMultimap(HashMultimap.create());
private static final ResponseEntity<ApolloConfigNotification>
NOT_MODIFIED_RESPONSE = new ResponseEntity<>(HttpStatus.NOT_MODIFIED);
@RequestMapping(method = RequestMethod.GET)
public class OpenEntityManagerInViewInterceptor extends EntityManagerFactoryAccessor implements AsyncWebRequestInterceptor {
@Override
public void afterCompletion(WebRequest request, Exception ex) throws DataAccessException {
if (!decrementParticipateCount(request)) {
EntityManagerHolder emHolder = (EntityManagerHolder)TransactionSynchronizationManager.unbindResource(getEntityManagerFactory());
logger.debug("Closing JPA EntityManager in OpenEntityManagerInViewInterceptor");
EntityManagerFactoryUtils.closeEntityManager(emHolder.getEntityManager());
}
}
public class DispatcherServlet extends FrameworkServlet {
protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
...
// Actually invoke the handler.
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
if (asyncManager.isConcurrentHandlingStarted()) {
return;
@RestController
@RequestMapping("/notifications")
public class NotificationControllerSample implements MessageListener {
@Autowired
private EntityManagerUtil entityManagerUtil;
private List<String> findWatchedKeys(String applicationId, String namespace,
String dataCenter) {
List<String> watchedKeys = Lists.newArrayList();
//do more logic, may need to load from db in some cases
@nobodyiam
nobodyiam / ApolloClientGetConfigDemo.java
Created July 6, 2016 14:25
Apollo client get config
Config config = ConfigService.getAppConfig();
Integer defaultRequestTimeout = 200;
Integer requestTimeout = config.getIntProperty("requestTimeout", defaultRequestTimeout);
@nobodyiam
nobodyiam / ApolloClientChangeListenerDemo.java
Created July 6, 2016 14:32
Apollo client change listener
Config config = ConfigService.getAppConfig();
config.addChangeListener(new ConfigChangeListener() {
@Override
public void onChange(ConfigChangeEvent changeEvent) {
for (String key : changeEvent.changedKeys()) {
ConfigChange change = changeEvent.getChange(key);
System.out.println(String.format(
"Found change - key: %s, oldValue: %s, newValue: %s, changeType: %s",
change.getPropertyName(), change.getOldValue(),
change.getNewValue(), change.getChangeType()));
@nobodyiam
nobodyiam / ApolloClientGetNamespaceConfigDemo
Created July 6, 2016 14:51
Apollo client get config of namespace
Config config = ConfigService.getConfig("FX.Hermes.Producer");
Integer defaultSenderBatchSize = 200;
Integer senderBatchSize = config.getIntProperty("sender.batchsize", defaultSenderBatchSize);
@nobodyiam
nobodyiam / ApolloClientNamespaceChangeListenerDemo.java
Created July 6, 2016 14:52
Apollo client change listener for namespace
Config config = ConfigService.getConfig("FX.Hermes.Producer");
config.addChangeListener(new ConfigChangeListener() {
@Override
public void onChange(ConfigChangeEvent changeEvent) {
System.out.println("Changes for namespace " + changeEvent.getNamespace());
for (String key : changeEvent.changedKeys()) {
ConfigChange change = changeEvent.getChange(key);
System.out.println(String.format(
"Found change - key: %s, oldValue: %s, newValue: %s, changeType: %s",
change.getPropertyName(), change.getOldValue(),
Escaper pathEscapers = UrlEscapers.urlPathSegmentEscaper();
Escaper queryEscapers = UrlEscapers.urlFormParameterEscaper();
Escaper fragmentEscaper= UrlEscapers.urlFragmentEscaper();
String path1 = "somePathWithSpace ";
String path2 = "somePathWith中文AndPlus+";
String someKeyword = "someKeywordWith中文AndPlus+AndSpace ";
String someFragment = "someFragmentWithSpace ";
String url = String.format(