2013年5月9日 星期四

Spring Cache AspectJ Setup

1. In pom.xml
<dependency>
  <groupid>org.springframework</groupid>
  <artifactid>spring-instrument</artifactid>
  <version>${org.springframework-version}</version>
</dependency>
<dependency>
  <groupid>org.springframework</groupid>
  <artifactid>spring-aspects</artifactid>
  <version>${org.springframework-version}</version>
</dependency>
<dependency>
 <groupid>org.aspectj</groupid>
 <artifactid>aspectjrt</artifactid>
 <version>1.7.2</version>
</dependency> 

2. In spring-context.xml
<context:load-time-weaver/>
<cache:annotation-driven mode="aspectj" />

3. Add JVM variable
 -javaagent:spring-instrument-3.x.x.RELEASE.jar
 (The file can be found in the maven directory)

4. Options: custom aop.xml
You may not want AspectJ try to weave all classes. Put your own aop.xml under META-INF like below

Look for more detail

<?xml version="1.0"?>

<!--
 AspectJ load-time weaving config file to install common Spring aspects.
-->

<aspectj>

<!-- Only weave the classes in willy.package -->
    <weaver options="-verbose -showWeaveInfo">
        <include within="willy.package.*" />
    </weaver>
    
    <!-- Below are the annotations used by spring-aspects -->
    <aspects>
        <aspect name="org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect" />
        <aspect name="org.springframework.scheduling.aspectj.AnnotationAsyncExecutionAspect" />
        <aspect name="org.springframework.transaction.aspectj.AnnotationTransactionAspect" />
        <aspect name="org.springframework.cache.aspectj.AnnotationCacheAspect" />
    </aspects>
</aspectj>



2013年5月7日 星期二

TCP Keep ALive TIdTCPServer TIdTCPClient [C++]

For TIdTCPServer:
#pragma comment(lib,"Ws2_32.lib")

//Set Keep-Alive
void __fastcall TForm5::IdTCPServer1Connect(TIdContext *AContext)
{
 struct tcp_keepalive {
  u_long  onoff;
  u_long  keepalivetime;
  u_long  keepaliveinterval;
 };
 tcp_keepalive live,liveout;
 live.keepaliveinterval=1000;
 live.keepalivetime=1000;
 live.onoff=TRUE;
  DWord dw ;
 if(WSAIoctl(AContext->Connection->Socket->Binding->Handle,IOC_IN|IOC_VENDOR|4,&live,sizeof(live),&liveout,sizeof(liveout),&dw,NULL,NULL) != 0){
  ShowMessage("Failed to set Keep-Alive");
 }
}

// Detect Abnormal Disconnection

void __fastcall TForm5::IdTCPServer1Disconnect(TIdContext *AContext)
{
 ShowMessage("Disconnect");
}
For TIdTCPClient
#pragma comment(lib,"Ws2_32.lib")
void __fastcall TForm5::IdTCPClient1Connected(TObject *Sender)
{
 DWord dw ;
 struct tcp_keepalive {
  u_long  onoff;
  u_long  keepalivetime;
  u_long  keepaliveinterval;
 };
 tcp_keepalive live,liveout;
 live.keepaliveinterval=1000;
 live.keepalivetime=1000;
 live.onoff=TRUE;

 if (WSAIoctl(IdTCPClient1->Socket->Binding->Handle,IOC_IN|IOC_VENDOR|4,&live,sizeof(live),&liveout,sizeof(liveout),&dw,NULL,NULL) != 0){
  ShowMessage("Failed to set Keep-Alive");
 };
}

// Detect Abnormal Disconnection
try{
        IdTCPClient1->IOHandler->Connected()    
}catch(Exception &e){
 ShowMessage(e.ClassName());  // Abnormal Disconnection
}

2013年3月13日 星期三

Jersey Client with Customize JSON (Jackson) Configuration


Below code allow user assign its own Jackson ObjectMapper with Jersey Client instead of the default one.


    
    ClientConfig clientConfig = new DefaultClientConfig();
    // Allow Jersey Client support JSON.
    clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);   
    // Create your own Jackson ObjectMapper.
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    // Create your own JacksonJaxbJsonProvider and then assign it to the config.
    JacksonJaxbJsonProvider jacksonProvider = new JacksonJaxbJsonProvider();
    jacksonProvider.setMapper(mapper);
    clientConfig.getSingletons().add(jacksonProvider); 

    Client client = Client.create(clientConfig);