Java tutorial
/* * Copyright (c) 2015-2020, Chen Rui * * 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 com.vnet.demo.service.azure.servicebus; import java.io.IOException; import java.net.URLEncoder; import java.util.Hashtable; import javax.jms.ConnectionFactory; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import org.springframework.beans.factory.config.PropertiesFactoryBean; import com.vnet.demo.utils.SpringUtil; public class AzureServiceBusServiceFactory { private static AzureServiceBusServiceFactory serviceFactory = null; private String username; private String password; private String host; private AzureServiceBusService azureServiceBusService; private AzureServiceBusServiceFactory() { } public synchronized static AzureServiceBusServiceFactory getInstance() { if (serviceFactory == null) { serviceFactory = new AzureServiceBusServiceFactory(); } return serviceFactory; } public AzureServiceBusService create() { if (this.azureServiceBusService == null) { try { PropertiesFactoryBean properties = SpringUtil.getBean(PropertiesFactoryBean.class); this.username = (String) properties.getObject().get("azure.servicebus.username"); this.password = (String) properties.getObject().get("azure.servicebus.password"); this.host = (String) properties.getObject().get("azure.servicebus.host"); String defaultQueue = (String) properties.getObject().get("azure.servicebus.queue"); String connectionString = "amqps://" + username + ":" + encode(password) + "@" + host; Hashtable<String, String> env = new Hashtable<String, String>(); env.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.qpid.amqp_1_0.jms.jndi.PropertiesFileInitialContextFactory"); env.put("connectionfactory.ServiceBusConnectionFactory", connectionString); Context context = new InitialContext(env); ConnectionFactory connectionFactory = (ConnectionFactory) context .lookup("ServiceBusConnectionFactory"); azureServiceBusService = new AzureServiceBusService(connectionFactory, defaultQueue); } catch (NamingException | IOException e) { e.printStackTrace(); } ; } return this.azureServiceBusService; } public static String encode(String encodee) { String retval = ""; try { retval = URLEncoder.encode(encodee, "UTF-8"); } catch (Exception e) { System.out.print("Encoding failed\n"); } return retval; } }