Wednesday, August 19, 2015

How to stop disconnecting Wi-Fi when locking a MAC.

Hi All,

I've been experiencing an issue where the Wi-Fi keeps disconnecting when I lock my screen on my MacBook. I found the following solution to keep it up always.

Find the network interface for Wi-Fi.

Use the "ifconfig" command to find the network interface. It would most probably be "en0" or "en1".

Disable disconnecting.

Run the following commands. In the 2nd command, make sure you use the correct network interface found in the earlier step. Running the following commands may ask for the password. If so, please provide.

cd /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources
sudo ./airport en1 prefs DisconnectOnLogout=NO

And thats all. Wi-Fi should be connected even if the screen is locked.

References...

[1] - http://apple.stackexchange.com/questions/71884/wi-fi-disconnects-when-i-lock-the-mac

Wednesday, August 12, 2015

Publishing WSO2 product logs to a Topic in Apache ActiveMQ.

Hi All,

This post will describe how the logs of a WSO2 product can be published to a topic in Apache ActiveMQ. WSO2 products use apache's log4j module for logging purposes. We will be using the "org.apache.log4j.net.JMSAppender" class to publish logs to a topic. ActiveMQ already has a guide on how this can be done [1].

log4j.properties

Add the following configuration to <WSO2_PRODUCT>/repository/conf/log4j.properties file.

log4j.appender.JMS_APPENDER=org.apache.log4j.net.JMSAppender
log4j.appender.JMS_APPENDER.InitialContextFactoryName=org.apache.activemq.jndi.ActiveMQInitialContextFactory
log4j.appender.JMS_APPENDER.Append=true
log4j.appender.JMS_APPENDER.ProviderURL=tcp://127.0.0.1:61616
log4j.appender.JMS_APPENDER.TopicBindingName=dynamicTopics/logTopic
log4j.appender.JMS_APPENDER.TopicConnectionFactoryBindingName=ConnectionFactory

We also have to add the "JMS_APPENDER" logger to the root logger in log4j.properties file. Modify the following line...

log4j.rootLogger=ERROR, CARBON_CONSOLE, CARBON_LOGFILE, CARBON_MEMORY, CARBON_SYS_LOG, ERROR_LOGFILE

To the following...

log4j.rootLogger=ERROR, CARBON_CONSOLE, CARBON_LOGFILE, CARBON_MEMORY, CARBON_SYS_LOG, ERROR_LOGFILE, JMS_APPENDER

Dependency JAR files.

Add the following jar files from <ACTIVEMQ_HOME>/lib to <WSO2_PRODUCT>/repository/components/lib folder.

  • activemq-broker-5.*.jar
  • activemq-client-5.*.jar
  • geronimo-jms_1.1_spec-1.1.1.jar
  • geronimo-j2ee-management_1.1_spec-1.0.1.jar
  • hawtbuf-1.*.jar

Reading received log messages.

The logs are published as JMS ObjectMessages from the JMSAppender class. So therefore we wont be able to see the content of it directly. We would need to create a customer client by using the activemq dependency which can be downloaded or used as a pom dependency from here. The jar file is also available at the <ACTIVEMQ_HOME>/ folder. Using this dependency jar and the code below, we can extract the contents of the log messages.

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQObjectMessage;
import org.apache.log4j.spi.LoggingEvent;

import javax.jms.Connection;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;

public class Log4jJMSAppenderExample implements MessageListener {

    public Log4jJMSAppenderExample() throws Exception {
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61616");
        final Connection conn = factory.createConnection();
        final Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
        conn.start();
        final MessageConsumer consumer = session.createConsumer(session.createTopic("logTopic"));
        consumer.setMessageListener(this);

        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                try {
                    consumer.close();
                    session.close();
                    conn.close();
                } catch (JMSException e) {
                    throw new RuntimeException(e);
                }
            }
        });
    }

    public static void main(String[] args) throws Exception {
        new Log4jJMSAppenderExample();
    }

    public void onMessage(javax.jms.Message message) {
        try {
            // receive log event in your consumer
            LoggingEvent event = (LoggingEvent) ((ActiveMQObjectMessage) message).getObject();
            System.out.println("Received log [" + event.getLevel() + "]: " + event.getMessage());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

Notes

If there is a need to use the jndi.properties file, we will have to place it at the <WSO2_PRODUCT>/ folder instead of the <WSO2_PRODUCT>/repository/conf/. The jndi.properties file is not necessary as we have defined the topic in log4j.properties as "dynamicTopics/logTopic".

I have tested the above with WSO2 ESB 4.8.1 and Apache ActiveMQ 5.11.1.

References...

[1] - http://activemq.apache.org/how-do-i-use-log4j-jms-appender-with-activemq.html
[2] - https://docs.wso2.com/display/ESB481/Configure+with+ActiveMQ

Sunday, August 2, 2015

Unzip all zip files in a folder in terminal.

Hi,

With the following command you can unzip all the zip files in the current folder.

unzip \*.zip

Removing __MACOSX folder from a zip file/archive.

Hi All,

Recently I created a zip file in MAC OS X. But when I copied it from my MAC OS X to my Ubuntu machine and opened it, theres a folder named "__MACOSX" inside. I have no idea how it got there.

But you can delete that folder by running the following command in MAC OS X after compressing.

zip -d filename.zip __MACOSX/\*