Tuesday, February 9, 2016

Making Deluge as the default torrent client on Linux Mint

Hi,

This post is about making "Deluge" as the default torrent client instead of "Transmission".

Run the following command to make "Deluge" the default client for magnet links.

xdg-mime default deluge.desktop x-scheme-handler/magnet

To make "Deluge" as the default torrent client, go through the following steps..

  • Right click on the torrent file.
  • Go to properties.
  • Go to "Open With" tab.
  • Select "Deluge" from the list and click "Set as default" button.

And thats all!, Happy downloding!

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/\*

Saturday, July 18, 2015

RabbitMQ .NET client to connect to WSO2 MB with SSL.

Hi All,

WSO2 Message Broker 3.0.0 is a distributed message broker that provides reliable messaging both secured and unsecured.

This post will explain on how the RabbitMQ .NET client can be used to publish or subscribe to WSO2 Message Broker 3.0.0 securely.

Creating the Certificate

Go to "<MB_HOME>/repository/resources/security" folder and run the following command. This will create a "cert" file which is the certification file for SSL communication. "wso2carbon.jks" is the trust store file. So we are exporting the certificate from the trust store. The "keytool" command comes with the JDK distribution.

keytool -export -keystore wso2carbon.jks -storepass wso2carbon -file carbon.cert -alias localhost -keypass wso2carbon

The Code

You can use the same implementation as mentioned in [1][2]. Only difference is we have to set the following properties to the "ConnectionFactory" object.

........
........
// The connection factory to connect with the broker.
ConnectionFactory factory = new ConnectionFactory();

// AMQP URL to connect to the broker.
IProtocol protocol = Protocols.AMQP_0_9_1;
factory.VirtualHost = "/carbon";
factory.UserName = "admin";
factory.Password = "admin";
factory.HostName = "localhost";
// Port for SSL
factory.Port = 8672;
factory.Protocol = protocol;

// SSL configuration
factory.Ssl.Enabled = true;
factory.Ssl.CertPassphrase = "wso2carbon";
factory.Ssl.CertPath = @"C:\Users\wso2\Documents\wso2mb-3.0.0-ALPHA\repository\resources\security\carbon.cert";
factory.Ssl.ServerName = "localhost";
factory.Ssl.AcceptablePolicyErrors = System.Net.Security.SslPolicyErrors.RemoteCertificateChainErrors;
factory.Ssl.Version = System.Security.Authentication.SslProtocols.Tls;

using (IConnection conn = factory.CreateConnection())
{
    ............
    ............

Set the correct path for "factory.Ssl.CertPath" to the "cert" file we generated in the earlier step.

Thats all guys!. Click here to download the sample .NET project(VS2010).

References...

[1] - https://docs.wso2.com/display/MB300/Publishing+and+Receiving+Messages+from+a+Queue
[2] - https://docs.wso2.com/display/MB300/Publishing+and+Receiving+Messages+from+a+Topic
[3] - https://www.rabbitmq.com/ssl.html
[4] - http://pathberiya.blogspot.com/2010_08_01_archive.html

Wednesday, July 15, 2015

RabbitMQ client to subscribe to a queue in WSO2 Message Broker 3.0.0 using Java

Hi All,

WSO2 Message Broker 3.0.0 is a distributed message broker that provides reliable messaging.

This post will explain on how the RabbitMQ java client can be used to subscribe to a queue WSO2 Message Broker 3.0.0 with the help of Maven.

Add the RabbitMQ java client dependency

Add the following dependency the pom.xml file

<dependency>
    <groupId>com.rabbitmq</groupId>
    <artifactId>amqp-client</artifactId>
    <version>3.5.3</version>
</dependency>

The Code

Following is the implementation. See descriptions inline.

// The queue name
String queueName = "MyQueue";

// Creating the AMQP connection string for communication
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
factory.setPort(5672);
factory.setVirtualHost("/carbon");
factory.setUsername("admin");
factory.setPassword("admin");
final Connection connection = factory.newConnection();
final Channel channel = connection.createChannel();

// Creating the queue
channel.queueDeclare(queueName, true, false, false, null);

// Binding the queue to "amq.direct" exchange. Exchanges cannot be declared in WSO2 MB 3.0.0.
channel.queueBind(queueName, "amq.direct", queueName);

// Creating consumer
QueueingConsumer consumer = new QueueingConsumer(channel);
channel.basicConsume(queueName, false, consumer);

// Shutdown hook handler
Runtime.getRuntime().addShutdownHook(new Thread() {
    @Override
    public void run() {
        try {
            // Closing the connection
            channel.close();
            connection.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (TimeoutException e) {
            throw new RuntimeException(e);
        }
    }
});

// Accepting messages
while (true) {
    QueueingConsumer.Delivery delivery = consumer.nextDelivery();
    String message = new String(delivery.getBody());

    System.out.println("Message Received : " + message);
}

Click here to download the sample maven project.

References...