|
| 1 | +/* |
| 2 | + * Copyright 2021-2023 Solace Corporation. All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.solace.samples.java.snippets; |
| 18 | + |
| 19 | +import com.solace.messaging.MessagingService; |
| 20 | +import com.solace.messaging.MessagingServiceClientBuilder; |
| 21 | +import com.solace.messaging.config.SolaceProperties; |
| 22 | +import com.solace.messaging.config.profile.ConfigurationProfile; |
| 23 | + |
| 24 | +import java.util.Properties; |
| 25 | + |
| 26 | +/** Sampler for enabling payload compression. */ |
| 27 | +public class HowToEnablePayloadCompression { |
| 28 | + /** |
| 29 | + * Example of how to configure payload compression for a service that is connected to a broker |
| 30 | + * running on localhost on port 55555. |
| 31 | + * |
| 32 | + * @param compressionLevel A compression level the range 0-9. |
| 33 | + * <p>Value meanings: |
| 34 | + * <ul> |
| 35 | + * <li>0 - disable payload compression (the default) |
| 36 | + * <li>1 - least amount of compression and fastest data throughput |
| 37 | + * <li>9 - most compression and slowest data throughput |
| 38 | + * </ul> |
| 39 | + * |
| 40 | + * @return a {@code MessagingService} instance connected to localhost:55555 with payload |
| 41 | + * compression level set to the given value. |
| 42 | + * @see SolaceProperties.ServiceProperties#PAYLOAD_COMPRESSION_LEVEL |
| 43 | + * @see MessagingServiceClientBuilder#fromProperties(Properties) |
| 44 | + */ |
| 45 | + public static MessagingService configurePayloadCompressionOnLocalHost(int compressionLevel) { |
| 46 | + Properties properties = new Properties(); |
| 47 | + properties.setProperty( |
| 48 | + SolaceProperties.ServiceProperties.PAYLOAD_COMPRESSION_LEVEL, |
| 49 | + String.valueOf(compressionLevel)); |
| 50 | + |
| 51 | + return MessagingService.builder(ConfigurationProfile.V1) |
| 52 | + .local() /* To connect to a broker located elsewhere remove this line and see HowToConfigureServiceAccessWithProperties. */ |
| 53 | + .fromProperties(properties) |
| 54 | + .build() |
| 55 | + .connect(); |
| 56 | + } |
| 57 | +} |
0 commit comments