Thursday, May 23, 2013

When Amazon SQS throws Exception..


When exception occurs when try to execute some data from AWS SQS we can rollback the changes/ do any other stuff like this

1. Write a call extending SimpleMessageListenerContainer

public class BoundryMessageListenerContainer extends SimpleMessageListenerContainer {
protected void rollbackOnExceptionIfNecessary(Session session, Throwable ex) throws JMSException {
try {
if (session != null & ex.getMessage() != null) {

JmsUtils.rollbackIfNecessary(session);

String message = ex.getLocalizedMessage();

if (message != null & message.length() > 0) {
// DO WHAT EVER YOU NEED TO DO 
}
}
} catch (IllegalStateException ex2) {
} catch (JMSException ex2) {
} catch (RuntimeException ex2) {
} catch (Error err) {
}
}
}

2. Add this to spring configuration

<bean id="container"
class="...........BoundryMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="messageListener" ref="listener" />
<property name="destination" ref="boundryQueue" />
</bean>

Number of messages in AWS SQS



1. Spring configuration

<bean id="amazonSQSClient" class="com.amazonaws.services.sqs.AmazonSQSClient">
<constructor-arg>
<bean class="com.amazonaws.auth.BasicAWSCredentials">
<constructor-arg value="${amazon.access.key}" />
<constructor-arg value="${amazon.access.secret}" />
</bean>
</constructor-arg>
</bean>

2. Write your java code as


@Autowired
AmazonSQSClient amazonSQSClient;

..


public int getNoOfMessagesInQueue() {
AmazonSQS sqs = amazonSQSClient;
sqs.setEndpoint(endPoint);
GetQueueAttributesRequest getQueueAttributesRequest = new GetQueueAttributesRequest(sqsUrl);
Collection<String> attributeNames = new ArrayList<String>();
attributeNames.add("All");
getQueueAttributesRequest.setAttributeNames(attributeNames);
GetQueueAttributesResult queueAttributes = sqs.getQueueAttributes(getQueueAttributesRequest);
return Integer.parseInt(queueAttributes.getAttributes().get("ApproximateNumberOfMessages"));
}

Monday, May 6, 2013

Write to separate Log files using Logback

There may be many instance you may have to write your logs to separate log files. Here are the easy steps to do that


  • Write a new logger in the existing log configuration file
<!-- Plain Text Rolling Appender for Custom logger file -->
    <appender name="SEPERATE_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <Append>true</Append>
        <File>seperate.log</File>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss} %c{1} [%p] %m%n</pattern>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>seperate.log.%d{yyyy-MM-dd}</fileNamePattern>
            <maxHistory>30</maxHistory>
        </rollingPolicy>
    </appender>
    
    <!-- additivity=false ensures seperate ata only goes to the seperate log -->
    <logger name="seperate" level="DEBUG" additivity="false">
        <appender-ref ref="SEPERATE_FILE"/>
    </logger>

  • Add the new logger where every you need in the java class as 
private static final Logger log = LoggerFactory.getLogger("seperate");
  • Starts logging as log.info..., log.debug..
  • Cool ha