Tuesday, February 4, 2014

Оптимизация кода uptolike.ru

Сегодня я поделюсь небольшим хаком по настройке социальных кнопок Uptolike!
Почему социальные кнопки и почему Uptolike? Все просто - этим сервисом я пользуюсь сам, но возможно, описанное ниже решение окажется полезным еще для кого-то.

Забегая немного вперёд, хочу сказать, что информация своего рода инсайдерская, возможно скоро и сами Uptolike прольют свет на такой способ размещения, но так приятно быть первым..

Tuesday, December 10, 2013

Tune spring remoting over http exporter

Noticed that using spring remoting over HttpInvokerProxyFactoryBean everything hangs when one of invokations is too long.

The problem is with SimpleHttpServerFactoryBean on server side. It turns out it handles requests on single thread by default. Solution:
package transport;

import org.springframework.remoting.support.SimpleHttpServerFactoryBean;
import java.util.concurrent.Executors;

public class ThreadedHttpServerFactoryBean extends SimpleHttpServerFactoryBean {

    public ThreadedHttpServerFactoryBean() {
        setExecutor( Executors.newFixedThreadPool( 50 ) );
    }
}

Here we gave 50 threads to http server.

Monday, November 25, 2013

im4java: float density parameter

IMOperation.density takes int as arguments, which is not a case. To pass double argument use:

double density = 144.65;
imOperation.addRawArgs( "-density", String.format( "%1.1f", density) );

Wednesday, October 30, 2013

Speed up tomcat

sudo mcedit /opt/site/conf/server.xml

find
    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               URIEncoding="UTF-8"
               redirectPort="8443" />

change to
    <Connector port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol"
               connectionTimeout="20000"
               URIEncoding="UTF-8"
               redirectPort="8443" />
done.



Friday, October 11, 2013

Migrating ehcache to spring 3.2.3

Spring 3.2.3 offered new cache annotations and xml constructions. Make sure you use
org.springframework.cache.annotation.Cacheable instead of com.googlecode.ehcache.annotations.Cacheable. The correct xml initialization:


    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
        <property name="cacheManager" ref="ehcache"/>
    </bean>

    <bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        <property name="configLocation" value="classpath:ehcache.xml"/>
        <property name="shared" value="true"/>
    </bean>

    <cache:annotation-driven cache-manager="cacheManager" />

This must be placed in contextLoader part of spring congiguration (specified by contextConfigLocation parameter in web.xml), not in webapp part! cache namespace:
xmlns:cache="http://www.springframework.org/schema/cache", location: http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd

Also add dependency on 'net.sf.ehcache:ehcache:2.7.4' instead of 'com.googlecode.ehcache-spring-annotations:ehcache-spring-annotations:1.2.0', 'cause we will use spring's annotation.


Tuesday, August 13, 2013

Saving url to binary file with firefox addon sdk

var {Cc, Ci, components, Cu} = require('chrome');

Cu.import("resource://gre/modules/PrivateBrowsingUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");

var persist = Cc['@mozilla.org/embedding/browser/nsWebBrowserPersist;1'].createInstance(Ci.nsIWebBrowserPersist);


var targetFile = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
targetFile.initWithPath("/tmp/logo.png");

persist.persistFlags = persist.PERSIST_FLAGS_FROM_CACHE | persist.PERSIST_FLAGS_REPLACE_EXISTING_FILES;

var uri = Cc["@mozilla.org/network/io-service;1"]
.getService(Ci.nsIIOService)
.newURI("https://developer.cdn.mozilla.net/media/img/mdn-logo-sm.png", null, null);

persist.saveURI(uri, null, null, null, "", targetFile, null);


  1. https://developer.mozilla.org/en-US/docs/Code_snippets/Downloading_Files
  2. https://developer.mozilla.org/en-US/docs/Code_snippets/Miscellaneous#Saving_an_arbitrary_URL_to_a_local_file
  3. https://addons.mozilla.org/en-US/developers/docs/sdk/latest/dev-guide/guides/xul-migration.html
  4. http://forums.mozillazine.org/viewtopic.php?f=19&t=2015813
Update: the same with filepicker dialog:

function getDownloadFile(defaultString, fileType) {
var nsIFilePicker = Ci.nsIFilePicker;

   var fp = Cc["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
   var window = require("sdk/window/utils").getMostRecentBrowserWindow();
   fp.init(window, "Save As", nsIFilePicker.modeSave);
   try {
     var urlExt = defaultString.substr(defaultString.lastIndexOf(".")+1, 3);
     if (urlExt!=fileType) defaultString += "." + fileType
   }catch(ex){}

   fp.defaultString = defaultString;

   fp.appendFilter(fileType, "*." + fileType);
   var rv = fp.show();
   if (rv == nsIFilePicker.returnOK || rv == nsIFilePicker.returnReplace) {
      var file = fp.file;
      var path = fp.file.path;
      return path;
   }
   return null;
}

var {Cc, Ci, components, Cu} = require('chrome');

Cu.import("resource://gre/modules/PrivateBrowsingUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");

var persist = Cc['@mozilla.org/embedding/browser/nsWebBrowserPersist;1'].createInstance(Ci.nsIWebBrowserPersist);


var targetFile = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);

const file = getDownloadFile("logo.png", "png");
if (!file) {
   return;
}
targetFile.initWithPath(file);

persist.persistFlags = persist.PERSIST_FLAGS_FROM_CACHE | persist.PERSIST_FLAGS_REPLACE_EXISTING_FILES;

var uri = Cc["@mozilla.org/network/io-service;1"]
.getService(Ci.nsIIOService)
  .newURI("
https://developer.cdn.mozilla.net/media/img/mdn-logo-sm.png", null, null);

persist.saveURI(uri, null, null, null, "", targetFile, null);



Wednesday, June 12, 2013

While using dev mode of GWT in Jetbrains IDEA, which runs jetty, I need to use spring instrumentation.
Default parameter -javaagent:spring-instrumentation.jar didn't work for me.
To run it wit Jetty you are to add additional parameter -Xbootclasspath. The final string:
-javaagent:/spring-instrument-3.2.0.RELEASE.jar -Xbootclasspath/a:spring-instrument-3.2.0.RELEASE.jar


Thanks to this:
http://gradle.1045684.n5.nabble.com/Java-Agent-LTW-problem-with-Gradle-and-Jetty-td4938600.html