Java tutorial
/* * Copyright 2009-2012 Alma Media Corporation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package mailmonk.client; import java.io.IOException; import java.util.Properties; import com.rabbitmq.client.*; /** * Main client to Mail Monk. * @author Kai Kousa * */ public class MailMonk { private String username; private String password; private String host; private String queueName = "mailmonk-jobs"; private Connection connection; private Channel channel; public MailMonk() { } /** * Creates an instance and sets it up from <code>Properties</code>. * Configuration looks for the following properties: * <ul> * <li>mailmonk.mq.username - your RabbitMQ username</li> * <li>mailmonk.mq.password - your RabbitMQ password</li> * <li>mailmonk.mq.host - host of you RabbitMQ instalation</li> * <li>mailmonk.mq.queueName - the desired queueName (mailmonk-jobs is the default, change this only if you need to)</li> * </ul> * */ public MailMonk(Properties p) { setup(p); } /** * Configure Mail Monk. * */ private void setup(Properties p) { setUsername(p.getProperty("mailmonk.mq.username")); setPassword(p.getProperty("mailmonk.mq.password")); setHost(p.getProperty("mailmonk.mq.host")); //if null, keep the default queue name if (p.getProperty("mailmonk.mq.queueName") != null) { setQueueName(p.getProperty("mailmonk.mq.queueName")); } } /** * Connect to a Message Queue and open a channel for * sending Mail Monk jobs. * */ public void connect() { try { ConnectionFactory factory = new ConnectionFactory(); factory.setUsername(getUsername()); factory.setPassword(getPassword()); factory.setHost(getHost()); connection = factory.newConnection(); channel = connection.createChannel(); channel.queueDeclare(getQueueName(), false, false, false, null); } catch (IOException e) { e.printStackTrace(); } } /** * Close connections to Message Queue * */ public void disconnect() { try { connection.close(); } catch (IOException e) { e.printStackTrace(); } } /** * Send a message to be processed by Mail Monk. * @param message * */ public synchronized void send(Message message) { try { String json = MessageSerializer.serialize(message); channel.basicPublish("", getQueueName(), null, json.getBytes()); } catch (IOException e) { e.printStackTrace(); } } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getHost() { return host; } public void setHost(String host) { this.host = host; } public String getQueueName() { return queueName; } public void setQueueName(String queueName) { this.queueName = queueName; } }