Follow up of create `KafkaServer` from Java
I am creating a KafkaServer from Java (well Clojure really but given a working Java example it is straightforward to translate).
I am not able to pass anything but an empty sequence. How can I write the equivalent of this line in Java?
https://github.com/apache/kafka/blob/cb674e5487f3f56647546b323dfe4fd45ccf0186/core/src/main/scala/kafka/server/KafkaServerStartable.scala#L27
val reporters = KafkaMetricsReporter.startReporters(new VerifiableProperties(serverProps))
Or, better yet, is there a Java/Clojure API for creating reporters?
The code I gave you in create KafkaServer from Java should work. I just tried it in 0.11.0.1 (the version you mentioned in the other question) and it works fine.
Full snippet:
package main;
import java.util.Properties;
import kafka.metrics.KafkaMetricsReporter;
import kafka.metrics.KafkaMetricsReporter$;
import kafka.utils.VerifiableProperties;
import scala.collection.*;
public class Reporters {
public static void main(String[] args) {
Properties props = new Properties();
Seq<KafkaMetricsReporter> reporters = KafkaMetricsReporter$.MODULE$.startReporters(new VerifiableProperties(props));
}
}
Related
I am trying to send data via usb with Java.And I decided to use the jSerialComm library. I downloaded the required jar file and imported it correctly.
The whole code :
import com.fazecast.jSerialComm.SerialPort;
public class Try{
public static void main(String[] args) throws IOException {
SerialComm ports[] = SerialComm.getCommPorts();
}
}
There is no problem with this row :
import com.fazecast.jSerialComm.SerialPort;
But there is a problem here :
SerialComm ports[] = SerialComm.getCommPorts();
Error message : SerialComm cannot be resolved to a type.
And this is advice : Create class 'SerialComm'
You
import com.fazecast.jSerialComm.SerialPort
But not
import com.fazecast.jSerialComm.SerialComm
SearialPort class is different than SerialComm, you need to import SerialComm class as well.
If you couldn't find it, it means your jar file is not compatible with your snippet code.
you should import this way :
import com.fazecast.jSerialComm.SerialComm
you could check out this if you want to more information :instalation
look at this is a example of send data via usb with Java
check out this :jSerialComm/package-summary
I want to use the Twilio API to allow users from my web application to make calls or send messages. So far I only wrote this basic code:
import com.twilio.sdk.TwilioRestClient;
import com.twilio.Twilio;
import com.twilio.rest.api.v2010.account.Call;
import com.twilio.type.PhoneNumber;
public class Main {
public static final String ACCOUNT_SID = "ACXX";
public static final String AUTH_TOKEN = "XX";
public static void main(String[] args) throws URISyntaxException {
Twilio.init(ACCOUNT_SID, AUTH_TOKEN);
Call call = Call.creator(new PhoneNumber("+40742000000"), new PhoneNumber("+40742000000),
new URI("http://demo.twilio.com/docs/voice.xml")).create();
System.out.println(call.getSid());
}
This is the place where my JAR is stored now(the end of Referenced Libraries)
It just says that the imports Twilio cannot be resolved. I have Java version 8, so it should be working like this. I also download the JARs and followed the instalation from this page. Still not working. Does any of you have an idea how to make it work?
Twilio developer evangelist here.
If you are using the version 7 Twilio Java library then you no longer need to import com.twilio.sdk.TwilioRestClient;. In fact, that is no longer there, so this might be causing your import issues.
Also, make sure you have only one version of the JAR in your project. And make sure to keep up to date, the current version, as of writing, is 7.14.4.
Check out the docs on making a call with Twilio in Java. You'll find the example looks like this:
import java.net.URI;
import java.net.URISyntaxException;
import com.twilio.Twilio;
import com.twilio.rest.api.v2010.account.Call;
import com.twilio.type.PhoneNumber;
public class Example {
// Find your Account Sid and Token at twilio.com/user/account
public static final String ACCOUNT_SID = "your_account_sid";
public static final String AUTH_TOKEN = "your_auth_token";
public static void main(String[] args) throws URISyntaxException {
Twilio.init(ACCOUNT_SID, AUTH_TOKEN);
Call call = Call.creator(new PhoneNumber("+14155551212"), new PhoneNumber("+15017250604"),
new URI("http://demo.twilio.com/docs/voice.xml")).create();
System.out.println(call.getSid());
}
}
Give that a go and let me know if it helps.
If anyone trying to use twilio [ I am using with spring boot ], please always make sure you pick up the latest version <version>7.50.1</version> as of now.
I used earlier version and it didn't work. As soon as I upgraded that to 7.50.1
it worked.
I hope it helps you.
I am a very much new to Netflix archaius. I have a code snippet which reads Java property file and prints property value.
When this program runs it prints the value of property named "Fields" from testproperty.properties file. Now while this program is running I am updating the value of "Fields" property, so archaius should fetch change value dynamically. But it is still printing older value.
What is the correct way to use archaius with this Java Program? Or to update properties in a program without restarting it ? If someone can point out correction in this code snippet it would be helpful.
I want to run a demo with Netflix archaius, so I have imported archaius through maven in my project.
Now I am updating my properties file. But still it prints the old property value. (P.S.: I have kept the continuous while loop in driver program to see if archaius picks the update property value runtime. I guess that's what archaius suppose to do. Fetching the updated property without restarting application. Correct me if I am wrong.)
Below is my code snippet :
import com.netflix.config.DynamicPropertyFactory;
import com.netflix.config.DynamicStringProperty;
public class PropertyChangetest {
public static void main(String args[]) {
DynamicPropertyFactory sampleProp = DynamicPropertyFactory.getInstance();
System.setProperty("archaius.configurationSource.defaultFileName", "TestProperty.properties");
System.setProperty("archaius.fixedDelayPollingScheduler.delayMills", "500");
while(true) {
DynamicStringProperty sampleProp1 = sampleProp.getStringProperty("fields","");
System.out.println(sampleProp1.get());
}
}
}
My "TestProperty.properties" file only have one property called fields. After running the program, I am updating my property file but it still prints older value.
The idea is to implement a custom PolledConfigurationSource, so Archaius can poll the source and update the property for consumption. I have also included a callback that the smart way to consume the property without your App polling it again (remember Archaius is doing the polling part for you).
Important note on the sample code : The program exits after the first callback. If you want to test more callbacks, increase the counter at class variable 'latch'
package com.test.config;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.junit.Test;
import com.netflix.config.AbstractPollingScheduler;
import com.netflix.config.ConcurrentMapConfiguration;
import com.netflix.config.ConfigurationManager;
import com.netflix.config.DynamicConfiguration;
import com.netflix.config.DynamicPropertyFactory;
import com.netflix.config.DynamicStringProperty;
import com.netflix.config.FixedDelayPollingScheduler;
import com.netflix.config.PollResult;
import com.netflix.config.PolledConfigurationSource;
public class TestArchaius {
CountDownLatch latch = new CountDownLatch(1);
#Test
public void tes() throws Exception {
AbstractPollingScheduler scheduler = new FixedDelayPollingScheduler(0, 1000, false);
DynamicConfiguration dynamicConfiguration = new DynamicConfiguration(new MyPolledConfigurationSource(), scheduler);
ConfigurationManager.install(dynamicConfiguration);
DynamicStringProperty fieldsProperty = DynamicPropertyFactory.getInstance().getStringProperty("fields", "");
fieldsProperty.addCallback(() -> {
System.out.println(fieldsProperty.get());
latch.countDown();
});
latch.await();
}
class MyPolledConfigurationSource implements PolledConfigurationSource {
#Override
public PollResult poll(boolean initial, Object checkPoint) throws Exception {
ConcurrentMapConfiguration configFromPropertiesFile = new ConcurrentMapConfiguration(
new PropertiesConfiguration("TestProperty.properties"));
Map<String, Object> fullProperties = new HashMap<String, Object>();
configFromPropertiesFile.getProperties().forEach((k, v) -> fullProperties.put((String) k, v));
return PollResult.createFull(fullProperties);
}
}
}
I ran into this issue recently. I wanted to implement a use case such as the DynamicPropertyFactory's prop should be updated via a REST API request. I had googled a lot on how to do that since the property was not getting updated after using the
.setProperty(String prop, String value) method. But I found that if a property is not defined before then the property gets added with the help of this method.
So the problem comes down like its not possible to override a property.
Also I found that it is using ConcurrentCompositeConfiguration for the Config instance and not the ConcurrentMapConfiguration. As I googled further found tabnine posts and found there is a method called
setOverrideProperty(java.lang.String key, java.lang.Object finalValue)
Override the same property in any other configurations in the list.
So casting to this class setting the override property resolved the issue. Property got updated successfully.
#Reference to ConcurrentCompositeConfiguration - http://netflix.github.io/archaius/archaius-core-javadoc/com/netflix/config/ConcurrentCompositeConfiguration.html
I have got a corrupted property file from customer. The file is modified by application to update version number. The code uses apache commons configuration. When I tested, the library always seems to write files in iso-8859-1 format.
Code is simplified to below. What is the possibility of following code write bad file?
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import java.io.FileWriter;
import java.io.IOException;
public class TestConfig {
public void editVersionInfo() throws ConfigurationException, IOException {
String filename = "C:\\temp\\VersionProperties\\Version.properties";
PropertiesConfiguration config = new PropertiesConfiguration(filename);
config.setProperty("application.version", "2011");
config.save(new FileWriter(filename));
}
public static void main(String[] args) throws IOException, ConfigurationException {
TestConfig tc = new TestConfig();
tc.editVersionInfo();
}
}
Just in case - the bad file looks like below. It does not look like in any encoding. The file originally was normal property file with keys and values all in English(ascii chars).
F????Co?aR??m??E?3#?? =
h\u00BD5j\u00B3\u00E0\u0096\u001D\u0081fe\u00BEo\b\u00A3\u0001\u00FE\u00A4\u00DE\u0000\u00FBi\"\u009C{\u00FC\u00D9\u00E2?c\u00F6\u00FF%B\u00A47\u00195\u001EXv\u0097/\u00D7x\u0099\u000E\u00A2gIX\u0014\u0097]k\u00882\u0003\u0014\u0097\u00BC\u00C3\u00AE\u00B4\u001E\u00B3R\u00E4\u00DE&\u0000\u0016\u009B\"7\u0085'\"\u00DCT*v'\u0092\u0007\u0091A\u00BD\u00ACl6~\u0097\u00C0\u00B1\u00D1\u00EB\u00FF\u00A8\u00F3\u0001'\u00BF\u0006\u001F\u009C\fk\u009F\u00C2\u00D9L^_\u0004J4\u00AF\u00D8\u00DAW\u00C4\u00CDj\u00E3\u0095\u00D1+\u00CE?\u0004>Z]\u00D7\u000B\u0098\u0016\u0095\u00AC\u00F7\u00E7\u009ATF\u0019\f)\u00A3\u00A9\u00DC\u00AD\u00ACtq5\u0085\u008E-\u00A3oH\u0000\u00C2\u0092\u00B5\u00F2\u008AG\u008F&\u00F5\u0017H\u0003!\u0083\u00B4\u008AV=\u00E0\u00EDj\u00F0\u00D0J\u00DB\u00CC\u00F2O\u00CE\u00BE\u00F0*4\u0006y~\u00C3\u00B7\"\u000B\u00E4\u00C0$>\u00F3\u00F2~\u00CE\u0097#\u00BAc\u00EC#\u00B4\u00AD\u009A\u00BAX\fF\u0083]\u00C2\u00D4\u00AB\u00F3\u009DQ\u0092\u00854z\u0097\u00FDG\t\u0095\u00E3}ty\u0082I\u00C3`\u009E
??
Edit: The customer environment is japanese. How ever the application is always run with
-Dfile.encoding=UTF8
I suspect your customer has a different default character encoding to what you have. Check their setting of the property file.encoding (counterintuitively named, I know).
An alternative possibility is that you have two threads writing that property file. I don't know, but I suspect the Apache library won't be thread-safe by default.
I'm having a problem with Eclipse and the content assistant regarding Hibernate.
As far as I understand, I've linked the Javadoc for hibernate-core-4.0.0.CR7.jar correctly (the validation goes through).
import org.hibernate.cfg.Configuration;
public class TestHibernate
{
Configuration config = new Configuration();
config.
}
Once I typed "config." however, I was not greeted with the content assistant. Can anyone offer some insight into what might be missing? Thanks!
You are not writing in a method body or an initialisation block.
import org.hibernate.cfg.Configuration;
public class TestHibernate
{
Configuration config = new Configuration();
public void someMethod() {
config. //Should work from here
}
}